Ch9: Parameter Passing
Outline
• Subprograms• Parameter Passing
– Parameter correspondence– Main Issues when designing subroutine in
programming languages– Parameter passing techniques
Characteristics of Subprogram• Each program has a single entry point• The calling program unit is suspended during
execution of the called subprogram, which implies that there is only on subprogram in execution
• Control always return to the caller when subprogram execution terminates
• The two fundamental types of subprograms are:– procedure– functions
Basic Definitions
• Structure or definition of a subprogram construct is:special-word name (list of parameter)
Fortran: subroutine Addr (parameters)Ada: procedure Addr (parameters)
C: void Addr (parameters)
Parameters• Formal parameters -> parts of the specification• Actual parameters
ex) formal parametervoid swap (first, second) {
int temp; temp = first;
first = second; second = temp;
} end swap-------------------- actual parameterswap (x, y);
Parameters• The parameters in the subprogram header are
called formal parameters.• Parameters include as arguments when the
subroutine is called are named actual parametersnote: a subroutine that return a value is called a function
• A subroutine that does not return a value is usually called a procedure or method
• Most programming languages require subroutine to be declared before they are used (forward)
Parameter Correspondence
Establishing the correspondence between actual parameters and formal parameters
1. Positional CorrespondenceThe correspondence is established by pairing actual and formal parameters based on the positions of the actual and formal parameter list.
Parameter Correspondence
2. Correspondence by explicit nameIn Ada the formal parameter is paired with each actual parameter they may be named explicitly in the calling statement.example:
sub (y => B, x=> 32); formal actual
Parameter Correspondence
3. Mixed keyword (Explicit name) and positional • Most languages that support keyword
parameters allow both: Ada, Fortran, Dylan, Python
• The first parameter in a list can be positional, and the remainder can be keyword parameters.ex)
my_sub (Length, sub=>B, List =>my_array);
Parameter Correspondence
4. Optimal parameters• Formal parameter list includes default
values to be used if the corresponding actual parameter is missing.
• This gives a very short way of writing certain kinds of overloaded function definitions
Example in C++
int f( int a =1, int b=2, int c=3) {body}
int f() { f(1, 2, 3); }int f( int a) { f(a, 2, 3); }int f( int a, int b) { f(a, b, 3); }int f( int a, int b, int c) { f(a, b, c); }
Unlimited Parameter Lists• Some languages allow actual parameter lists of
unbounded length: C, C++, and scripting languages like JavaScript, Python, and Perl
• Library routines must be used to access the excess actual parameters
• A hole in static type systems, since the types of the excess parameters cannot be checked at compile time
int printf (char *format, .....) { body }
Example in Ada
function Compute_Pay (Income : Float; Exemptions : Integer := 1;
Tax_Rate : Float) return Float;
Pay := Compute_Pay (2000.0, Tax_Rate => 0.15);
Main Issues when designing subroutine in programming languages
• What parameter passing methods are used?• Are the types of the actual parameters checked
against the types of the formal parameters?• Check for error condition• Are local variables statically or dynamically
allocated?• Can subroutines definitions appear on other
subroutines definitions?
Main Issues when designing subroutine in programming languages (cont)
• Can subroutines can be passed as parameters?• Can subroutines be nested?• If passed as parameters and/or nested, what is
the referencing environment of a passed parameter?
• Can a subroutine be generic?
Parameter Passing Technique 1Pass-by-Value• The value of the actual parameter is associated
with the formal parameter• It is initialized using the value of the
corresponding actual parameter, before the called method begins executing
• Simplest method• Widely used• The only method in real Java• Changing to a formal parameter do not affect the
actual parameter
Changes Visible to the Caller• When parameters are passed by value, changes
to a formal do not affect the actual • But it is still possible for the called method to
make changes that are visible to the caller• The value of the paramter could be a pointer( in
Java, a reference)• Then the actual cannot be changed, but the
object referred to by the actual can be
Example for Pass by Value (1)int plus(int a, int b) { a += b; return a;}
void f() { int x = 3; int y = 4; int z = plus(x, y);}
CurrentActivation record
a : 3
b : 4
return address
Previous Activation record
Result
x : 3 y : 4 z : ?
return address
PreviousActivation record
When plusIs starting
Example for Pass by Value (2)
name formal parameter type result
function square(x : integer) : integer begin
square := x * x end
square(2) => 4
Example for Pass by Value (3)procedure swap(a, b); integer a, b, temp; begin temp
temp := a; a := b; b := temp; end; y
swap(x, y); x effect
Parameter Passing Technique 2Pass-by-Result• The formal parameter is just like a local variable
in the activation record of the called procedure( it is uninitialized)
• After the called method finish execution, the final value of the formal parameter is assigned to the corresponding actual parameter
• It is also called copy-out• It was introduced in ALGOL 68• Sometimes used in Ada
Example for Pass by Resultvoid plus (int a, int b, by-result int c){ c = a + b;}
void f(){ int x = 3; int y = 4; int z; plus(x, y, z);}
current AR
a : 3
b : 4
c : ?
RA
Previous AR
x : 3
y : 4
z : ?
RA
Previous AR
1
When plus is starting
Example cont.
current AR
a : 3
b : 4
c : 7
RA
Previous AR
x : 3
y : 4
z : ?
RA
Previous AR
x : 3
y : 4
z : 7
RA
Previous AR
2 3when plus is ready to return
When plushas returned
current AR
Parameter Passing Technique 3
Pass-by-Value Result• The formal parameter is just like a local variable
in the activation record of the called procedure• After the called method finish execution, the final
value of the formal parameter is assigned to the corresponding actual parameter
• It is also called copy-in/copy-out• It is initialized using the value of the
corresponding actual parameter, before the called method begins execution
Example for Pass by Value Result
void plus(int a, by-value-result int b){ b += a;}
void f(){ int x = 3; plus(4, x);}
current AR a : 4
b : 3
RA
PAR
x : 3
RA
PAR
1When plusis starting
Example cont.
current AR
a = 4
b = 7
RA
PAR
x = 3
RA
PAR
x = 7
RA
PAR
2 3When plus isready to return
When plushas returned
Parameter Passing Technique 4
Pass-by-Reference• The address of the actual parameter is
passed to the called method. This address is used as the formal parameter.
• The formal parameter is an alias for the formal parameter
• One of the earliest methods: Fortran• Most efficient for large object• Still frequently used
Picture
x reference value
b reference value
x reference value
b reference
copy-incopy-out
Example for Pass by Reference (1)
void plus (int a, by-reference int b) { b += a;}
void f() { int x = 3; plus(4, x);}
CAR
a = 4
b
RA
PAR
x = 3
RA
PAR
CAR
a = 4
b = 7
RA
PAR
x = 7
RA
PAR
11 When plus Is starting
2When plus isReady to return
Implementing Referencevoid plus( int a, by-reference int b) {
b += a;} previous examplevoid f() {
int x = 3;plus( 4, x );}
void plus (int a, int *b) {*b += a;
}void f() { C implementation
int x = 3;plus(4, &x); By-reference = address by value
}
Aliasing• When two expressions have the same lvalue,
they are aliases of each other• There are obvious cases:
ConsCell x = new ConsCell (0, null);ConsCell y = x;
A[ i ] = A[ j ] + A[ k ];
• Passing by reference leads to less obvious cases...
Example for Pass by Reference (2)
void sigsum( by-reference int n, by-reference int ans) {ans = 0;int I = I;while ( i <= n) ans +=i ++;
}
Int f() { int g() {int x, y; int x;x = 10; x = 10;sigsum (x, y); sigsum (x, x);return y; return x;
} }
Example contvoid sigsum(by-reference int n, by-reference int ans) {
ans = 0;int i = 1;while (i <= n) ans += i++;
}int g() {
int x;x = 10;sigsum(x, x);return x;
}
CAR
n :ans :i : ?RA
PAR
X : 10
RAPAR
Result : ?
When sigsumIs starting
Parameter Passing Technique 5Pass-by-Name• Each actual parameter is evaluated in the
caller’s context, on every use of the corresponding formal parameter
• Introduce in ALGOL 60• Now unpopular• The actual parameters is treated like a “Little
anonymous function” (Thunk)• Whenever the called method needs the value of
the formal, it calls the Thunk to get it.
Example for Pass by Name (1)procedure swap(a, b); integer a, b, temp; begin temp := a; a := b; b := temp; end
5 3 11 10 12 . . . . A
0 1 2 3 4
A[3] = 10 I = 2
Example 1 cont.swap(I, A[3]){ temp := 2 I := A[3] = 10 A[10] := temp}
swap(I, A[I]){ temp := 2 I := A[2] = 10 A[10] := temp}
Example for Pass by Name (2)void f( by-name int a, by-name int b){ b = 5; b = a;}
int g(){ int i = 3; f ( i + 1, i ); return;}
CAR
a:
b:
RA
PAR
i = 3
RA
PAR
result?
i
i + 1 Thunk
(Final value of a is 6)
Thunk
when f() is starting
Jensen’s DeviceReal procedure sum(j, lo, hi, Exp) Value lo, hi; Real Exp; Integers j, lo, hi; Begin Real RTN; RTN := 0; FOR I = lo step 1 UNTIL hi do RTN := RTN + Exp; SUM : = RTM End
x[ i ] * i
Jensen’s Device Exampley =
i = 1
10
3x - 5x + 22
y = sum(x, I, 10, 3 * x * x - 5 * x + 2)
Each time through the loop, evaluation of the expressionis actually the evaluation of
3x - 5x + 22
Procedure swap (a, b); Integer a, b, temp; Begin temp := a; a := b; b := temp; End
Jensen’s Device Example cont.
Effect of calling swap(i, x[i]) Before the call: i = 2, x[2] = 5What do we expect: i = 5, x[2] = 2After the call: i = 5, x[2] = 5, x[5] = 2
side effect
i = 2 x[i] = 5
temp 2i A[i] = 2A[i] = A[5] = 2
ThunksFunction XjThunk() : Real address VAR Expi : Real Begin Expi := x[ i ] * i XjThunk := address(Expi) End
Specification Issues
• Are these just implementation techniques, or part of the language specification?
• Depends on the language:– Without side-effects, parameter-passing
technique may be undetectable by the programmer
– Even with side effects, some languages specify the parameter passing technique only partially
Without Side Effects
• Big question are parameters always evaluated, or only if they are really needed?
• Cost model may also be used by the programmer– Is re-evaluation of a formal expensive?– Does parameter-passing take time
proportional to the size of the object?
With Side Effects
• A program can detect which parameter-passing technique is being used by the language system
• But it may be an implementation detail that programs are not supposed to depend on: it may not be part of the specification of the language
• Case in point: Ada