Sub topic 5
Parameter passing: value, reference, copy-restore and name
This chapter assumes you know what an activation record is and how the calling sequence stores arguments in it. It also assumes you can read C-style code. Here we ask what the callee receives for each parameter and how its changes reach the caller.
Terms
- A formal parameter is the name in the function definition.
- An actual parameter is the expression at the call site.
- An l-value is an expression that denotes a memory location, such as
a[i]. An r-value is just a value, such as3. - An alias is a second name for the same location.
The four modes
| Mode | What the callee gets | Callee writes reach caller? | Typical use |
|---|---|---|---|
| Value | A copy of the r-value, held in the callee's frame | No | C, Java primitives |
| Reference | The l-value's address | Immediately, through the alias | C++ `&`, Fortran |
| Copy-restore | A copy in, then a copy back out at return | Only at return | Some Fortran compilers, RPC |
| Name | The expression itself, re-evaluated at every use | Yes, at each assignment | Algol 60 |
Call by value
The caller evaluates the actual parameter and stores the result in the formal's slot in the callee's activation record. The callee changes only its own copy. Passing a pointer by value still lets the callee modify the pointed-to data. The pointer is copied, but both copies point to the same location.
Call by reference
The caller passes the address of the actual parameter. Every access to the formal is an indirect access through that address. If the actual is not an l-value, such as x + 1, the compiler stores the value in a temporary and passes that temporary's address. The address is fixed at call time. For a[i] it is computed once, even if the callee later changes i.
Call by copy-restore
The caller copies the value in, as with call by value. When the callee returns, the final value of each formal is copied back into the actual's location. This is also called copy-in copy-out or value-result. It matches reference when there are no aliases, and it differs when the callee reads or writes the actual through another name during the call.
Call by name
Conceptually the actual parameter replaces every occurrence of the formal in the body, and it is evaluated in the caller's environment. Compilers implement this with a thunk, a small parameterless function that evaluates the expression. Each use of the formal calls the thunk, so a[i] is recomputed with the current i.
Worked example: one function, four answers
int a = 1;
void f(int x) {
x = x + 1;
a = a + x;
}
/* call: f(a); then a holds the result */- Value:
xis 1, then 2. Thena = 1 + 2 = 3. Finala = 3. - Reference:
xis an alias ofa. Afterx = x + 1,ais 2. Thena = 2 + 2 = 4. Finala = 4. - Copy-restore:
xis 1, then 2. Thena = 1 + 2 = 3. On returnx(2) is copied intoa, so finala = 2. The copy-out overwrites the callee's change toa. - Name:
xis replaced bya, so the body becomesa = a + 1; a = a + a;. That gives2, then4. Finala = 4.
Where reference and name differ
Name and reference agree on simple variables. They differ when evaluating the actual parameter again would give a different location. The classic test is swap(i, a[i]).
void swap(x, y) { t = x; x = y; y = t; }Start with i = 1, a[1] = 2, a[2] = 7. This Python program emulates both modes with getter and setter functions standing in for thunks.
def swap(get_x, set_x, get_y, set_y):
t = get_x()
set_x(get_y())
set_y(t)
def make_env():
return {"i": 1, "a": {1: 2, 2: 7}}
def vars_i(env):
return (lambda: env["i"], lambda v: env.__setitem__("i", v))
# Call by name: a[i] is re-evaluated with the current i at every use.
env = make_env()
gi, si = vars_i(env)
swap(gi, si,
lambda: env["a"][env["i"]],
lambda v: env["a"].__setitem__(env["i"], v))
print("name: ", env)
# Call by reference: the address of a[i] is computed once, at call time.
env = make_env()
gi, si = vars_i(env)
k = env["i"]
swap(gi, si,
lambda: env["a"][k],
lambda v: env["a"].__setitem__(k, v))
print("reference:", env)The output is:
name: {'i': 2, 'a': {1: 2, 2: 1}}
reference: {'i': 2, 'a': {1: 1, 2: 7}}Under name, i becomes 2 before y = t runs, so y now means a[2]. Under reference, y stays bound to a[1]. Call by value gives no visible change to i or a.
Edge cases and common errors
- Aliased actuals. For
g(x, y) { x = 1; y = 2; }called asg(a, a), reference leavesa = 2. Copy-restore copies back both formals, so the result depends on the restore order. Left to right gives 2, and right to left gives 1. - Reading the global during the call. Reference shows the callee's writes to the actual at once, and copy-restore does not. The worked example above relies on this.
- Side effects under name. An actual such as
a[i++]runs its side effect once per use of the formal, not once per call. - Arrays in C. An array argument decays to a pointer, so the callee can change the caller's elements. This is call by value of an address, not call by reference.
- Cost. Value copies large structures. Reference copies only an address but adds an indirection on every access. Name needs a thunk call per use, which is the slowest.
Exercise
Global a = 3. Procedure g(x) { x = x + 1; a = a * 2; } is called as g(a). Give the final value of a under call by value, reference, copy-restore and name.
Show answer
Value: x = 4, then a = 6, so a = 6. Reference: x aliases a, so a = 4, then a = 8, so a = 8. Copy-restore: x = 4, a = 6, and on return x (4) is copied into a, so a = 4. Name: the body becomes a = a + 1; a = a * 2, so a = 8.
Exercise
With i = 1 and a = [_, 2, 7] (1-indexed), what is the final state after swap(i, a[i]) under call by reference, where swap is the three-statement swap shown above?
Show answer
The address of a[1] is fixed at call time. t = 1, then i = a[1] = 2, then a[1] = t = 1. The result is i = 2, a[1] = 1, a[2] = 7.