Determining the Scope of Parameters

Using Global Objects defined the scope of a declaration as all the blocks in a program with access to an object declared in the program. That section explained how the scope of a declaration extends through nested blocks in a program.

You can use parameters to pass an object to a procedure that does not fall within the scope of the declaration of that object. This fact makes parameters a more general tool for IPC than global objects. A parameter can increase the scope of an object in the following ways:

  • An object can be passed as a parameter to a procedure that is not nested within the block that declares the object.

  • A parameter can be passed to an external procedure, whether the procedure is a passed external procedure, a library procedure, or a separate program.

The objects a procedure can access, because the objects are declared in the procedure or are declared globally to the procedure, are referred to as the direct addressing environment of the procedure. The objects in the direct addressing environment, together with any objects passed as parameters to the procedure, comprise the extended addressing environment of the procedure.

If a procedure is passed as a parameter to another procedure, the invoked procedure gains access to the passed procedure. However, the scope is extended only one way. The passed procedure does not automatically gain access to objects declared in the invoked procedure.

The following ALGOL example includes two cases where the scope of a declaration has been increased by the effects of parameter passing:

Example

100 BEGIN
110
120 PROCEDURE P(Q);
130   PROCEDURE Q (R);
140     REAL R;
150     FORMAL;
160 BEGIN
170   REAL A;
180   A:= 2;
190   Q(A);
200   DISPLAY (STRING(A,*));
210 END;
220
230 PROCEDURE Y;
240 BEGIN
250   PROCEDURE X(Z);
260   REAL Z;
270   BEGIN
280     Z:= Z * 2;
290   END;
300
310   P(X);
320 END;
330
340 Y;
350
360 END.

Case 1

Procedure X cannot be directly invoked by a statement in procedure P, because the declaration of procedure X occurs within procedure Y. However, the statement at line 310 that invokes procedure P passes procedure X as an actual parameter to the formal parameter Q. Thus, the statement at line 190, which invokes the formal parameter Q, actually results in an invocation of procedure X. In this way, a statement in procedure P is able to invoke a procedure outside the direct addressing environment of procedure P.

Case 2

Real variable A cannot be directly accessed by a statement in procedure X because A is declared within procedure P. However, the statement at line 310 passes procedure X as an actual parameter to formal parameter Q of procedure P. The statement in procedure P at line 190 then passes A as a parameter to procedure Q, thus making it possible for procedure X to access A.

Even after being passed to P, X does not automatically have access to objects declared in P. Thus, X could not have accessed A if A had not been passed as a parameter to X.