Home | Projects | Notes > Computer Architecture & Organization > Passing Parameters via the Stack
Parameters are passed in higher order languages (HOL) by:
Value
A copy of the actual argument's value is made in memory.
i.e., The caller and callee have two independent variables with the same value. If the callee modifies the parameter value, the effect is NOT visible to the caller.
Reference
The address of the passed argument is copied into the formal parameter.
Inside the function, the address is used to access the actual argument used in the call.
If the callee modifies the parameter value, the effect is visible to the caller.
Pointers in C
xxxxxxxxxx
31int x = 12; // sets aside one .word for variable x
2int *y = &x; // sets aside one .word as a pointer to integer y
3 // but no allocation for y
Pass-by-value example
xxxxxxxxxx
121void swap(int a, int b)
2{
3 int temp = a;
4 a = b;
5 b = temp;
6}
7
8void main(void)
9{
10 int x = 2, y = 3;
11 swap(x, y);
12}
Before making the call to swap
the values for x
and y
are pushed onto the stack and when it returns back to the main
it pulls the values back off the stack.
swap
will work only within the stack frame which will eventually go away when swap
returns.
Pass-by-reference example
xxxxxxxxxx
121void swap(int *a, int *b)
2{
3 int temp = *a;
4 *a = *b;
5 *b = temp;
6}
7
8void main(void)
9{
10 int x = 2, y = 3;
11 swap(&x, &y);
12}
Before making the call to swap
the values for x
and y
are pushed onto the stack and when it returns back to the main
it pulls the values back off the stack.
The values x
and y
are swapped because the swapping was done by using their addresses.
When you have a programming language that supports recursion, there are a lot of things that have to be accounted for. Some of this is not easy and will cause some really bad problems if not done right.
e.g., Dividing a number by 10
using recursion in the function utoa
.