livingmylivetothefullest

It's about life

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.

5. Consider the following program written in C syntax:
void swap(int a, int b) {
int temp;
temp = a;
a = b;
b = temp;
}

void main() {
int value = 2, list[5] = {1, 3, 5, 7, 9};
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
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.

a. 2, 1, 3, 5, 7, 9 b. 1, 2, 3, 5, 7, 9 c. 1, 2, 3, 5, 7, 9

2, 1, 3, 5, 7, 9 2, 3, 1, 5, 7, 9 2, 3, 1, 5, 7, 9

2, 1, 3, 5, 7, 9 5, 1, 3, 2, 7, 9 5, 1, 3, 2, 7, 9 (unless the addresses of the

actual parameters are

recomputed on return, in

which case there will be an

index range error.)

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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Information

This entry was posted on June 28, 2013 by and tagged .
June 2013
M T W T F S S
« Apr   Sep »
 12
3456789
10111213141516
17181920212223
24252627282930

Blog Stats

  • 6,909 hits
%d bloggers like this: