The idea: the mode decides what goes into the callee's frame
When main calls swap(i, a[i]), a new frame is made for swap.
Its parameters x and y must be filled with something. The
parameter passing mode decides with what: a copy of the value, the address of the
variable, or the argument expression itself, not yet evaluated. That choice changes what the same
procedure body does to the caller's variables.
How to read the page
- Source: the program, with the statement that runs now in yellow.
- Main's frame: the caller's variables
i,a[1..3],candr, each at an address. They start at i = 1, a = [3, 5, 7], c = 0 on every call. Variables the program does not use are faded. - The callee's frame:
x,yand, for swap, the localt. Green cells hold values or addresses, purple cells hold thunks. - An orange box is a value on its way. Yellow cells are the ones the current statement read.
- Results: one cell per mode and program, so you can compare all fifteen runs.
By value and by reference
Call by value copies the argument's value into the parameter. The body works on the
copy, so swap(i, a[i]) swaps two copies and main sees nothing change: i = 1,
a = [3, 5, 7]. (Demo: by value, the swap changes nothing)
Call by reference puts the address of the argument in the parameter, and every read
and write goes through it. The address is computed once, at the call:
a[i] with i = 1 becomes @104, the address of a[1], and stays that for the whole
call. So the swap works: i = 3, a = [1, 5, 7]. (Demo: swap(i, a[i]) by reference, hop by hop)
Pass by Value vs Pass by Reference shows the same two modes in
Java, Python and C++ terms, with mutation and copies of objects.
Value-result and aliasing
Call by value-result (also called copy-in copy-out) copies the value in at the call, works on the copy, and copies it back to the argument's address at the return. Most of the time it gives the same answer as by reference: swap(i, a[i]) ends with i = 3, a = [1, 5, 7] both ways.
The two differ when two names reach the same variable, which is called aliasing. In
p(i, i) with body x = x + 1; y = y + 10;:
| Mode | x = x + 1 | y = y + 10 | At return | i after the call |
|---|---|---|---|---|
| by reference | i becomes 2 | i becomes 12 | nothing | 12 |
| by value-result | copy x becomes 2 | copy y becomes 11 | i = 2, then i = 11 | 11 |
| by value | copy x becomes 2 | copy y becomes 11 | nothing | 1 |
By reference, x and y are both i, so the second addition sees the first. By value-result they are two separate copies, and the last copy written back wins. (Demo: aliasing p(i, i), reference vs value-result)
By name: the argument is re-evaluated at every use
Call by name, from Algol 60, does not evaluate the argument at all. It passes the
expression, packed with the frame it belongs to, as a small function called a
thunk. Every time the body uses the parameter, the thunk runs again, in
main's frame. When the body assigns to the parameter, the thunk is evaluated as a place and
the value is written there.
That is why swap(i, a[i]) goes wrong by name:
t = x: x isi, so t = 1.x = y: y isa[i]with i = 1, which is 3. x isi, so i = 3.y = t: y isa[i]again, but i is 3 now, so the place isa[3]. a[3] = 1.
The result is i = 3, a = [3, 5, 1]: a[1] never changed and a[3] was
overwritten. A swap procedure that works for every argument cannot be written with call by name.
(Demo: by name, the wrong element changes) The same trick, used on purpose, is called
Jensen's device: sum(i, 1, n, a[i]) can add up a[1] to a[n] because
a[i] is re-evaluated after each change of i.
By need: a thunk that remembers its value
A by-name thunk may run many times. Call by need runs it at most once: the first use evaluates it and stores the value in the thunk, later uses read that cache. This is the lazy evaluation of Haskell. It only gives the same answer as by name when evaluating the argument has no side effects, so it belongs to languages without assignment. The page refuses an assignment to a by-need parameter, so swap and p stop with an error under by need.
The program r = f(next(), 1/0), with next() adding 1 to a counter c and
returning it, and f(x, y) returning x + x when x > 0, shows both lazy
modes and the strict ones:
| Mode | What happens | Result |
|---|---|---|
| by value, reference, value-result | both arguments are evaluated before the call; next() gives 1, then 1/0 fails | error, c = 1 |
| by name | x is used three times (x > 0, then x + x), so next() runs three times: 1, then 2 + 3 | r = 5, c = 3 |
| by need | next() runs at the first use, gives 1; the other two uses read the cache | r = 2, c = 1 |
In both lazy modes y is never used, so 1/0 is never evaluated and the call succeeds. The
strict modes are called eager or strict; by name and by need are
lazy. (Demo: f(next(), 1/0) by name vs by need, Demo: f(next(), 1/0)
by value fails at the call)
Which languages use which mode
| Mode | Where you meet it |
|---|---|
| by value | C, Java, Python, JavaScript (for a reference to an object, the reference is the value copied) |
| by reference | C++ int&, Pascal var, C# ref, Fortran |
| by value-result | Ada in out for scalar types, some Fortran compilers |
| by name | Algol 60; Scala => T parameters; C macros behave alike (textual) |
| by need | Haskell; lazy val in Scala |
See also C++ pointers for what an address parameter is in memory, Recursion to an explicit stack and Tail calls for how frames are pushed and popped, and MIPS procedure calls for how arguments travel in registers at the machine level.
See also Static vs Dynamic Scoping for how a free name inside the callee is found, along the static chain or the call chain.
See also Exceptions and stack unwinding in Java for the third way to leave a call: a throw that unwinds frames.
What the page leaves out
The page passes every thunk with main's frame, because every call here comes from
main; which frame a free name refers to in general is a scoping question. It leaves out
result-only parameters (Ada out), the order in which copy-out happens when a language does
not fix it (Ada leaves it to the compiler; the page uses x then y), how real thunks are compiled as
closures, the address of an array element passed by value-result when the index changes during the
call, and how Haskell's evaluator updates thunks in place in the heap. The addresses are made up.