Call-by-Name Parameters

When a parameter is passed by name, the system never creates the formal parameter. Instead, the system substitutes the actual parameter for the formal parameter wherever the formal parameter is mentioned in the procedure.

The effect of passing by name is simplest in cases where the actual parameter is a simple variable. When the procedure accesses the formal parameter, the effect is as if the procedure were using a global variable. Any changes made to the value of the formal parameter immediately affect the value of the actual parameter and vice versa. This feature makes call-by-name parameters a useful means of communicating information between an asynchronous process and its initiator.

When an actual parameter that is a constant or an expression is passed by name, the compiler generates a thunk. A thunk (also known as an accidental entry) is a piece of code that evaluates the actual parameter and assigns the resulting value to the formal parameter. The system substitutes the thunk for the formal parameter wherever the formal parameter is mentioned in the procedure.

Thunks can be undesirable because they slow execution of the program and affect the definition of the critical block. (Critical blocks are discussed in Understanding Interprocess Relationships.) The programmer can prevent the creation of a thunk by passing each element of the expression as a separate parameter.

If a constant is passed by name, then whenever the value of the formal parameter is read, the formal parameter returns the value of the constant. The value of the formal parameter cannot change. An attempt to assign a value to the formal parameter results in a run-time error.

The effect of passing an expression by name varies, depending on whether the expression evaluates as a reference to a single object. For example, A[I] evaluates into a reference to a single element of array A. In this guide, such an expression is referred to as a simple expression. Other examples of simple expressions are the POINTER function in ALGOL and references to character-based record fields. On the other hand, an expression such as A + B does not evaluate as a reference to a single element. Such an expression is referred to as a complex expression.

For a simple expression, the system passes a thunk that reevaluates the expression each time the parameter is used in the procedure. For example, suppose the actual parameter A[I] is passed to the formal parameter F. At the time of the procedure invocation, I has a value of 5. The formal parameter F becomes a reference to element 5 of array A. When F is read, it reflects the most recent value of A[5]. When F is assigned, it changes the value of A[5]. If I is then assigned a value of 10, F becomes a reference to A[10]. Thereafter, reading or assigning F really accesses the value stored in A[10].

For a complex expression, the system passes a thunk that reevaluates the expression each time the formal parameter is read in the procedure. However, it is impossible to assign a value to the formal parameter; any attempt to do so results in a run-time error.