Sie sind auf Seite 1von 14

Parameter Passing

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

1. procedure exchange(i , j : integer);


2. var x: integer;
3. begin
4. x:=a[i]; a[i]:=a[j]; a[j]:=x;
5. end
Parameter Passing Methods

• 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]

Storage location Required value


or or
l-value r-value

Here decision of whether to use the location or the value


represented by an expression is determined by whether
expression appears on the left or right, respectively, of the
assignment symbol.
Differences between parameter -passing methods
are mainly based on whether an actual parameter
represents an r-value, an l-value, or the text of the
actual parameter itself.
Call-by-value

Simplest method of passing parameters.


The actual parameters are evaluated by caller and
their r-values are passed to callee.
The callee may change the parameters, but this has
no effect on the caller.
A procedure called-by-value can affect its caller
through
 nonlocal
 pointer
Used in c and passcal
Call-by-value implementation

1. Formal parameter is treated like a local name


and stored in the activation record of the called
procedure.

2. The caller evaluates the actual parameters and


places their r-values in the storage for the
formals..
Parameter passing example
1) program reference( input, output );
2) var a, b: integer;
3) procedure swap(x, y: integer );
4) var temp : integer;
5) begin
6) temp := x;
7) x := y;
8) y := temp;
9) end;
10) begin
11) a := 1; b := 2;
12) swap( a, b );
13) writeln( ‘a = ‘, a ); writeln( ‘b = ‘, b )
14) end.

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

Das könnte Ihnen auch gefallen