Professional Documents
Culture Documents
// function example :
Begin
If A<= B then
Return A ;
Else
Return B ;
End if ;
End minimum;
Pass by value
Pass by result
Pass by value result
Pass by reference
Pass by name
When parameter is passed by value, the value of the actual parameter is used
to initialize the corresponding formal parameter, which then act as a local
variable in the subprogram
Pass by value is some times call pass by copy
Advantage: simple mechanism, actual parameter can be expression or values,
(formal parameters are write protected) does not cause side effect.
Disadvantage: needs additional storage for formal parameters.
int main() a b c x y
{ 2 5 10 2 5
int a, b, c;
a = 2; 50 10 10
b = 5;
c = multiply(a,b); 5
a = multiply(b,c);
return 0;
50
}
int multiply (int x, int y) When the program ends, the variables
{ remaining in memory have the values
x = x * y; shown in red
return x;
}
PRINCIPLES OF PROGRAMMING LANGUAGES 17
Limitations of pass by value
Recall that a function can have either one return value or no
return value
void swap (int x, int y) The function appears to work correctly. The next step is to
{ write a program that calls the function so that we can test it:
int tmp = x;
x = y;
y = tmp; int main()
} {
int a=2, b=6;
cout << “Before swap, a=” << a << “ and b=”
Output: << b << endl;
Before swap, a=2 and b=6 swap(a,b);
After swap, a=2 and b=6 cout << “After swap, a=” << a << “ and b=”
<< b << endl;
return 0;
}
PRINCIPLES OF PROGRAMMING LANGUAGES 19
What went wrong?
In the swap function, parameters x and y were passed the
values of variables a and b via the function call swap(a, b);
Then the values of x and y were swapped
When the function returned, x and y were no longer in
memory, and a and b retained their original values
Remember, when you use pass by value, the parameter only
gets a copy of the corresponding argument; changes to the
copy don’t change the original.
int multiply (int x, int y) void multiply (int x, int y, int *z)
{ {
x = x * y; *z = x * y;
return x; }
}
Pass by value result share with pass by value and pass by result the disadvantage of
requiring multiple storage for parameters and time for copying values.
Pass by value result share with pass by result the problems associated with the order in
which actual parameters are assigned.
Stack
Multiple memory uses on computer:
OS memory needs; e.g. ½ for Windows
Program code
User program data
Heap
Function invocations
Temporaries
Static Data
I/O buffers
Code
etc.
Different requirements, caused by differences in: lifetime, Reserved Space
Stack needed for data that are pushed and popped dynamically, following a last-in, first-
out pattern
Space needed at moment of function call, freed at moment of return
Allocation and de-allocation can be implemented cheaply, by adjusting stack pointer;
though “old” data remain in memory
More efficient use of space than static allocation
Most newer imperative languages use stack storage for data associated with activations;
became popular with Algol60
Upon Returning
The Callee:
places return value at place the caller can access
restores caller’s AR pointer and other registers, using saved info in stack marker
returns control to the caller
The Caller
can copy the returned value into its own AR
On some architectures: frees space for actuals
Local variables
Parameters
Dynamic link
Return address
Runtime Stack
PRINCIPLES OF PROGRAMMING LANGUAGES 36
ARI Example for a C Function
Local sum
void sub (float total,
int part) { Local list 4
… Local list 1
Local list 0
}
Parameter part
Parameter total
Dynamic link
Return address
Top
Local t
Local s
Parameter r
ARI
for B Dynamic link
Static link
Return (to MAIN)
ARI Local p
for MAIN
Top
Local y
Parameter x
ARI Dynamic link
for A
Static link
Return to B
Top
Local t
Local t
Local s Local s
Parameter r Parameter r
ARI ARI
for B Dynamic link for B Dynamic link
Static link Static link
Return (to MAIN) Return (to MAIN)
ARI Local ARI Local
p p
for MAIN for MAIN
int factorial(int n) {
if (n <= 1)
return 1
else
Functional value
return (n * factorial(n-1));
Parameters 3 }
}
Main
Main
Main 3
e.g. the first recursive call creates a second activation within the
lifetime of the first activation.
the part of the program text where the declared identifier is used.
Declaration of X
Declaration of Y
Use of X
Use of Z Hidden to A
Central problem:
the meaning of variable names, i.e. the correspondence between
names and memory locations.
Direct transmission
Referencing through a named data object
Example: x = y + 2*z;
The scope of a variable is the portion of the source program where the data item can be
accessed (i.e., the name of the item is a legal reference at that point in the program).