CSI311: Notes on Parameter Passing


NOTES  ON  PARAMETER - PASSING  MECHANISMS



Names,  l-values,  r-values

 {
  int I,J;
  int A[];

  J = 3;  
  I = J + 1;
  A[I] = I;
      .
      .
      .
 }

   name          l-value             r-value
              (environment)          (store)

===================================================
                                          ____________
     I           05834726                |     4      |
                                          ------------
                                          ____________
     J           05834727                |     3      |
                                          ------------
                                          ____________
     A[1]        05834728                |            |
                                          -----------
                                          ____________
                                         |            |
                                          ------------
                                          ____________
                                         |            |
                                          -------------
                                          ____________
     A[4]        05834731                |     4      |
                                          ------------
                                          ____________
     3           00000001                |     3      |
                                          ------------

PASS BY VALUE

* One-way communication * Provides input to the called procedure * Protects the actual parameters from change When a procedure is called: 1. An environment and a store are allocated for the called procedure, including the formal parameters and locally declared variables. 2. The r-values of the actual parameters are copied into locations specified by the l-values of the formal parameters. When control returns from a called procedure: 1. The environment and store for the called procedure are deallocated. PASS BY VALUE program I : integer; A : array[1..100] of integer; procedure VAL_SWAP(value X, Y : integer); var temp : integer; begin temp := X; X := Y; Y := temp; end; begin {main program} I := 3; A[I] := 6; write('I =', I); A[3]); writeln('A[3] =', A[3]); VAL_SWAP(I, A[I]); { --->>> temp:=X; X:=Y; Y:=temp;} write('I =', I); A[3]); writeln('A[3] =', A[3]); end. OUTPUT I = 3 A[3] = 6 I = 3 A[3] = 6 PASS BY RESULT * One-way communication * Provides output to the calling procedure * Protects the formal parameters om initialization by the calling routine When a procedure is called: 1. An environment and a store are allocated for the called procedure, including the formal parameters and locally declared variables. 2. The l-values of the actual parameters are saved, one for each of the formal parameters. When control returns from a called procedure: 1. The r-values of the formal parameters are copied into locations specified by the saved l-values of the actual parameters. 2. The environment and store for the called procedure is deallocated. PASS BY RESULT program I : integer; A : array[1..100] of integer; procedure RES_SWAP(result X, Y : integer); var temp : integer; begin temp := X; X := Y; Y := temp; end; begin {main program} I := 3; A[I] := 6; write('I =', I); writeln('A[3] =', A[3]); RES_SWAP(I, A[I]); { --->>> temp:=X; X:=Y; Y:=temp;} X:=Y; Y:=temp; write('I =', I); A[3]); writeln('A[3] =', A[3]); end. OUTPUT I = 3 A[3] = 6 * * * ERROR : UNDEFINED VARIABLE X PASS BY VALUE-RESULT * Two-way communication * Provides both input to, and output from the called procedure * No protection for formal or actual parameters When a procedure is called: 1. An environment and a store are allocated for the called procedure, including the formal parameters and locally declared variables. 2. The l-values of the actual parameters are saved, one for each of the formal parameters. 3. The r-values of the actual parameters are copied into locations specified by the l-values of the formal parameters. When control returns from a called procedure: 1. The r-values of the formal parameters are copied into locations specified by the saved l-values of the actual parameters. 2. The environment and store for the called procedure is deallocated. PASS BY VALUE-RESULT program I : integer; A : array[1..100] of integer; procedure V_R_SWAP(val_res X, Y : integer); var temp : integer; begin temp := X; X := Y; Y := temp; end; begin {main program} I := 3; A[I] := 6; write('I =', I); A[3]); writeln('A[3] =', A[3]); V-R_SWAP(I, A[I]); { --->>> temp:=X; X:=Y; Y:=temp;} X:=Y; Y:=temp; write('I =', I); A[3]); writeln('A[3] =', A[3]); end. OUTPUT I = 3 A[3] = 6 I = 6 A[3] = 3 PASS BY REFERENCE * Two-way communication * Provides both input to, and output from the called procedure * No protection for formal or actual parameters * Almost equivalent to value-result parameters; discrepancies show up in the presence of aliasing. When a procedure is called: 1. An environment and a store are allocated for the called procedure, including the formal parameters and locally declared variables. 2. The l-values of the formal parameters are set equal to the l-values of the actual parameters. When control returns from a called procedure: 1. The environment and store for the called procedure is deallocated. PASS BY REFERENCE program I : integer; A : array[1..100] of integer; procedure REF_SWAP(var X, Y : integer); var temp : integer; begin temp := X; X := Y; Y := temp; end; begin {main program} I := 3; A[I] := 6; write('I =', I); A[3]); writeln('A[3] =', A[3]); REF_SWAP(I, A[I]); { --->>> temp:=X; X:=Y; Y:=temp;} X:=Y; Y:=temp; write('I =', I); A[3]); writeln('A[3] =', A[3]); end. OUTPUT I = 3 A[3] = 6 I = 6 A[3] = 3 PASS BY NAME * One or two-way communication. If an actual parameter has no l-value, then assignment to the corresponding formal parameter is disallowed. * Delayed evaluation of actual parameters - by need only. * No protection for formal or actual parameters When a procedure is called: 1. An environment and a store are allocated for the called procedure, including the locally declared variables. 2. The literal text of the actual parameters replaces each occurrence of the corresponding formal parameters. Variables mentioned in these expressions are global to the called procedure. 3. If necessary, local variables are given new names not appearing elsewhere in the program. When control returns "from" a called procedure: 1. The environment and store for the called procedure is deallocated. PASS BY NAME program I : integer; A : array[1..100] of integer; procedure NAM_SWAP(name X, Y : integer); var temp : integer; begin temp := X; X := Y; Y := temp; end; begin {main program} I := 3; A[I] := 6; write('I =', I); A[3]); writeln('A[3] =', A[3]); NAM_SWAP(I, A[I]); { --->>> temp:=I; I:=A[I]; A[I]:=temp; } write('I =', I); A[3]); writeln('A[3] =', A[3]); end. OUTPUT I = 3 A[3] = 6 I = 6 A[3] = 6

The following three programs in BASIC, FORTRAN, and AlgolW look quite similar. But they all produce different output. Why?

The answer lies in subtle differences in the semantics of the three languages. In particular, the conventions involving the scopes of variables and the parameter passing mechanisms are different in the various languages. If we simulate all three programs in Pascal, then the differences become apparent.

In Pascal, variables must be declared local; they are taken from the "nearest" global context in which they were declared local otherwise.

Parameters must be declared as "var" parameters if the pass by reference mechanism is to be used. Otherwise, they default to pass by value. Pass by name is not directly expressible in Pascal, but the effect is simulated with code specific to the AlgolW example.

BASIC, FORTRAN, & ALGOLW Simulations BASIC 10 I = 1 : J = 2 : K = 3 20 D := FNW(I, J-2) 30 PRINT I 100 DEF FNW(L, M) 110 L = 10 : J = 6 120 PRINT M : FNW = 0 140 FNEND 999 END output -> 0 1 program basic(input, output); var I, J, K : integer; D : real; function W(L, M : integer) : real; begin L := 10; J := 6; writeln(M); { executable Pascal } W := 0 end; begin I := 1; J := 2; K := 3; D := W(I, J-2); writeln(I) end. FORTRAN I = 1 FUNCTION W(L, M) J = 2 L = 10 K = 3 J = 6 D = W(I, J-2) WRITE(4, 15) M 15 WRITE(4, 15) I W = 0 FORMAT(1X, I7) END END output -> 0 10 program fortran(input, output); var I, J, K : integer; D : real; temp : integer; function W(var L, M : integer) : real; var J : integer; begin L := 10; J := 6; writeln(M); { executable Pascal } W := 0 end; begin I := 1; J := 2; K := 3; temp := J-2; D := W(I, temp); writeln(I) end. ALGOL W INTEGER I,J,K; REAL D; I := 1; J := 2; K := 3; D := W(I, J-2); WRITE I; REAL PROCEDURE W(INTEGER L, M); BEGIN L := 10; J := 6; WRITE M; 0 END; output -> 4 10 program algolw(input, output); var I, J, K : integer; D : real; function W(L, M : integer) : real; begin {L is replaced by I} I := 10; J := 6; writeln({M is replaced by J-2} J-2); W := 0 { executable Pascal } end; begin I := 1; J := 2; K := 3; D := W(I, J-2); writeln(I) end.