Beruflich Dokumente
Kultur Dokumente
Shashwat Shriparv
dwivedishashwat@gmail.com
InfinitySoft
Parameter Passing
When one procedure calls another, the
communication between them occurs through:-
• Nonlocal names
• Parameter of called procedure
• Call-by-value
• Call-by-reference
• Copy-restore
• Call-by-name (Macro expansion)
Why so many
Parameter
Passing Methods ?
Different methods arise from
differing interpretations what
an expression represent
Consider an assignment :-
a[i] : = a[j]
10
Call by reference
• The caller passes the called procedure a POINTER to
the storage address of the actual parameter.
• If the actual has an l-value, it is used.
• If the actual parameter is an expression, the result of the
expression is evaluated in a new location and address of
that location is passed.
• Pascal uses call by reference if the “var” keyword is
used.
• C++ uses call by reference if the “&” operator is
specified.
11
Parameter passing example
1) program reference( input, output );
2) var a, b: integer;
3) procedure swap( var x, y: integer );
4) var temp : integer;
5) begin
6) temp := x;
7) x := y; Specifies call-by-
8) y := temp; reference
9) end;
10) begin
11) a := 1; b := 2;
12) swap( a, b );
13) writeln( ‘a = ‘, a ); writeln( ‘b = ‘, b )
14) end.
12
Copy restore
• This is a hybrid between call-by-value and call-by
reference.
• Before callee is activated, we evaluate the actuals and
put their r-values in the activation record for the callee.
• But we also compute and save the l-values of the
actuals.
• In the return sequence, we copy the updated r-values
from the callee’s activation record to the location for
the saved values. FORTRAN used this approach.
13
Call-by-name (macro expansion)
• In this method, we just substitute the body of
the procedure for the procedure call.
• In the copied body, the formal parameters are
replaced by the text of the actuals.
• #define macros in C/C++ use this technique.
Shashwat Shriparv
dwivedishashwat@gmail.com
InfinitySoft
14