Parameters and Parameter Transmission

Read: Pratt & Zelkowitz, Section 7.3.1 (pp. 312-323 only)


In the definition (declaration) of a subprogram such as (in C)

int f (int x, float y) { ... }
x and y are called the formal parameters of function f, and we often just call them "parameters", rather than "formal parameters". (Actually, it is the value of x and the value of y that are the parameters to f, but we rarely use the complete precise terminology because it becomes too cumbersome.) In an invocation of f such as
k = f(n, z);
(the values of) n and z are called the actual parameters or arguments of f. If the distinction is not crucial, the term "parameter" is sometimes used to mean either a formal parameter or an actual parameter.

Parameter transmission methods

We will now look at some of the different relationships between formal and actual parameters. That is, we will look at some of the different ways that the actual parameter is transmitted to a subprogram and the implications of the transmission methods in terms of their effects on the results of subprogram execution.

The formal names of the parameter transmissions are "call by" followed by the name of the mechanism, for example "call by value". However, the terminology "pass by" followed by the name of the mechanism, for example "pass by value", is used at least as often as the formal name.

The following definition of function f will be used in the examples, (using C syntax but not C parameter transmission semantics):

int x;
void f ( int a, int b) {
printf ("%d %d %d\n", a, b, x);
a = b + x;
printf ("%d %d %d\n", a, b, x);

Some of the basic parameter transmissions are listed and discussed below.

Call by name

Call by value

Call by reference

Call by result

Call by value-result

Call by constant value

Implementation of parameter transmission methods

Figure 7.12 in the text illustrates a standard implementation for pass by value and pass by reference. The diagram in this figure illustrates partial contents of the runtime stack for the program in Figure 7.11 just before the termination (return) of procedure (subprogram) Q. Figure 7.12 shows that for a value parameter (which is the default in Pascal, the syntax that is used in the example), a copy of the argument value is made into the activation record, and that a location (or locations) in the activation record is used in the same way that a local variable is used. For reference parameters (which are denoted by var in Pascal), the address of the argument value is passed, and all accesses to the reference parameter value are indirect through the address that is passed. Thus, call by reference is the same as passing a pointer by value (e.g., in C), and then having the subprogram automatically dereference the pointer each time that it is used.

Except for call by name, the other parameter passing mechanisms have similar implementations. Call by result requires a local memory cell (or cells) to hold the result that is to be copied back to the calling program, plus a memory cell to hold the address where the result is to be copied at subprogram termination. (That is, in call by result, one extra memory cell is required for the address.) Call by value-result is the same as call by result, except that the location of the result value is initialized by copying in the value of the argument.

Call by name (or delayed evaluation) is more difficult because the argument value (actually the argument address) must be obtained every time that it is referenced. So the compiler has to generate an anonymous function that returns the address of the argument, and the subprogram calls this function in order to obtain the argument address each time that the argument is used in the subprogram. (Such an anonymous address-calculation function is called a thunk.)