Friday, June 28, 2013

KBP - Chapter 9

Review Question

1.   What are the three general characteristics of subprograms?

• Each subprogram has a single entry point.
• The calling program unit is suspended during the execution of the called subprogram, which implies that there is only one subprogram in execution at any given time.
• Control always returns to the caller when the subprogram execution terminates.

2.   What does it mean for a subprogram to be active?

A subprogram is said to be active if, after having been called, it has begun execution but has not yet completed that execution.

7. What is a parameter profile? What is a subprogram protocol?

The parameter profile of a subprogram is the container of the number, order, and types of its formal parameters. While a subprogram protocol is subprogram’s parameter profile as well as its return type if it’s a function.

8.   What are formal parameters? What are actual parameters?

Formal parameters are The parameters in the subprogram header. They are sometimes thought of as dummy variables because they are not variables in the usual sense.
Actual parameters are a list of parameters to be bound to the formal parameters of the subprogram.

9.   What are the advantages and disadvantages of keyword parameters?

The advantage of keyword parameters is that they can appear in any order in the actual parameter list.
The disadvantage to keyword parameters is that the user of the subprogram must know the names of formal parameters.

15. What are the three semantic models of parameter passing?

The three semantic models of parameter passing are:

In mode: formal parameters can receive data from te corresponding actual parameter
Out mode: formal parameters can transmit data to the actual parameter
Inout mode: do both of In mode and Out mode.
20. What is the parameter-passing method of Python and Ruby called?

The parameter-passing method of Python and Ruby is called passing-by-assignment. Because all data values are objects, every variable is a reference to an object. So, the actual parameter value is assigned to the formal parameter. Quite similar to pass-by reference.

23. What is automatic generalization?

Automatic generalization is the case in F# when for some functions F# infers a generic type for the parameters and the return value. This happens when the type inferencing system in F# cannot determine the type of parameters or the return tye of the function.

24. What is an overloaded subprogram?

An overloaded subprogram is a subprogram that has the same name as another subprogram in the same referencing environment.

34. What is a closure?

A closure is a subprogram and the referencing environment where it was defined.


___________________________________________________
Problem Set

3.   Argue in support of the templated functions of C++. How is it different from the templated functions of other languages?

It is different as C++ differentiates function based on overloading. It is not practical to make multiple function overloading in regard to writability and readability. Instead, creating a template allows a function to receive any datatype as long as the variation is based on the formal parameter definition.

7.   Consider the following program written in C syntax:
void fun(int first, int second){
first+=first;
second+=second;
}

void main(){
int list[2] = { 3 , 5 };
fun(list[0], list[1]);
}
 for each of the following parameter-passing methods, what are the values of the list array after execution?
a. Passed by value :  list[2] = {3,5}
b. Passed by reference : list[2] = {6,10}
c. Passed by value-result : list[2] = {6,10}

15. How is the problem of passing multidimensional arrays handled by Ada?

Ada compilers are able to determine the defined size of the dimension of all arrays that are used as parameters at the time of subprograms are compiled.

No comments:

Post a Comment