Chapter 9

Subprograms

Assignment from Tri Djoko Wahjono

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.

2. What does it mean for a subprogram to be active?
If it has been called and started execution but the execution is not completed yet

3. What is given in the header of a subprogram?
Specifies that the following syntactic unit is a subprogram definition of some particular kind, provides name of the subprogram, optionally specifies a list of parameters

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.

6. What is a Ruby array formal parameter?
Substitute of keyword parameter which Ruby does not support

7. What is a parameter profile? What is a subprogram protocol?
Parameter profile contains number, order and types of its formal parameter. Subprogram Protocol is the parameter profile and the return value if it is a function.

8. What are formal parameters? What are actual parameters?
Formal parameters are parameters in the subprogram header. Actual parameters are a list of parameters included in subprogram call statements

9. What are the advantages and disadvantages of keyword parameters?
The advantage is they can appear in any order in the actual parameter list. The disadvantage is the user must know the names of formal parameters.

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.

Problem Set

2. In most Fortran IV implementations, parameters were passed by reference, using access path transmission only. State both the advantages and disadvantages of this design choice.
The main advantage of this method is the fast accesses to formal parameters in subprograms. The disadvantages are that recursion is rarely useful when values cannot be passed, and also that a number of problems, such as aliasing, occur with the method.

4. Suppose you want to write a method that prints a heading on a new output page, along with a page number that is 1 in the first activation and that increases by 1 with each subsequent activation. Can this be done without parameters and without reference to nonlocal variables in Java? Can it be done in C#?
This can be done in both Java and C#, using a static (or class) data member for the page number.

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
b. Passed by reference
c. Passed by value-result
Assume the calls are not accumulative; that is, they are always called with the initialized values of the variables, so their effects are not accumulative.

8. Argue against the C design of providing only function subprograms.
It is rather weak, but one could argue that having both adds complexity to the language without sufficient increase in writability.