Jumat, 28 Juni 2013

Chapter 9

Chapter 9 


Review Questions

1.    What are the three general characteristics of subprograms?



  • Each subprogram has a single entry point, excluding co-routine.
  • The calling program 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.
  • They provide flexibility to the subprogram
  • The storage of local variables in an active subprogram can be shared with the local variables in all inactive subprograms.
  • They efficiently used when computer has small memory (Faster Access).
  • Cost of the time required to allocate
  • Access to dynamic local variable must be indirect
  • The stack dynamic local variables, subprograms cannot be history sensitive



4.     What are formal parameters? What are actual parameters?
The parameters in the subprogram header are called formal parameters.
Subprogram call statements must include the name of the subprogram and alist of parameters to be bound to the formal parameters of the subprogram. These parameters are called actual parameters.
5.     What are the advantages and disadvantages of keyword parameters?
The advantage of keyword parameter 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.
7. What is a parameter profile? What is a subprogram protocol?Answer: Parameter profile is the number, order, and types of its formal parameters.
Subprogram protocol is its parameter profile plus, if it is a function, its return type. In languages in which subprograms have types, those types are defined by the subprogram’s protocol.

8. What are formal parameters? What are actual parameters?Answer: Formal parameters are the parameters in the subprogram header.
Actual parameters are a list of parameters to be bound to the formal parameters of the subprogram which must be included with the name of the subprogram by the subprogram call statements.

10 . What are the differences between a function and a procedure ?
- Functions return values while procedure does not .
- Procedure defines new statements, while function define new user-defined operators.
17.  What is parametric polymorphism?
Parametric polymorphism is provided by a subprogram that takes a generic parameter that is used in a type expression that describes the types of the parameters of the subprogram. Both Ada and C++ provides a kind of compile-time parametric polymorphism.

18.  What causes a C++ template function to be instantiated?
C++ template functions are instantiated implicitly either when the function is named in a call or when its address is taken with the & processor.

25. What is ad hoc binding?
Ad hoc binding is when the environment of the call statement that passed the subprogram as an actual parameter.
26. What is multicast delegate?
All of the methods stored in a delegate instance are called in the order in which they were placed in the instance. This is called a multicast delegate.

34. What is a closure?Closure is a nested subprogram ant its referencing environment, which together  allow the subprogram to be called from anywhare in a program

37. In what way coroutine different from conventional subprogram? coroutine control mechanism is often called symmetric unit control model, in the otherhand conventional subprogram have a master-slave relationship between a caller and a called subprogram.
coroutine can have multiple entry point which are controlled by the coroutine themselves.

Problem Set

3. Argue in support of the template functions of C++. How is it different from the template functions in other languages?Answer: C++ templated classes are instantiated to become typed classes at compile time. For example, a templated Count class, can be created with the following declaration:
Count<int> doCount;


5.Consider the following program writen in C syntax:void swap(int a, int b){
int temp;
temp = a;
a = b;
b = temp;
}
void main() {
int value = 1, list[5] = {2, 4, 6, 8, 10};
swap(value, list[0]);
swap(list[0], list [1]);
swap(value, list[value]);
}

For each of the following parameter-passing methods, what are all of the values of the variables value and list after each of the three calls to swap ?
a. Passed by Value
value =1 , list[5] = {2,4,6,8,10}
b. Passed by reference
value =6, list[5] ={4,1,2,8,10}
c. Passed by value-result
value =6, list[5] ={4,1,2,8,10}

6 . Compare and contrast PHP’s parameter passing with that of C#.
PHP’s parameter passing is similar to that of C#, except that either the actual parameter or the formal parameter can specify pass-by-reference. Passby- reference is specified by preceding one or both of the parameters with an ampersand

15. How is the problem of passing multidimensional arrays handles by Ada?
Ada compilers are able to determine the defined size of the dimensions of all arrays that are used as parameters at the time subprograms are compiled. In Ada, unconstrained array types can be formal parameters. An unconstrained array type is one in which the index ranges are not given in the array type definition. Definitions of variables of unconstrained array types must include index ranges. The code in a subprogram that is passed an unconstrained array can obtain the index range information of the actual parameter associated with such parameters

Tidak ada komentar:

Posting Komentar