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.

Main's frame holds i = 1 at @100 and a[1] = 3 at @104. Five versions of swap's frame for the call swap(i, a[i]): by value x = 1, y = 3 (copies); by reference x = @100, y = @104 (addresses); by value-result x = 1, y = 3 with copy-out addresses @100 and @104; by name x = thunk { i }, y = thunk { a[i] }; by need the same thunks with an empty cache
For the same call, the five modes put five different things into x and y.

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], c and r, 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, y and, for swap, the local t. 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;:

Modex = x + 1y = y + 10At returni after the call
by referencei becomes 2i becomes 12nothing12
by value-resultcopy x becomes 2copy y becomes 11i = 2, then i = 1111
by valuecopy x becomes 2copy y becomes 11nothing1

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)

Two timelines for p(i, i), top to bottom. By reference: x and y both point to i; x = x + 1 makes i 2; y = y + 10 makes i 12; result 12. By value-result: copy in x = 1, y = 1; x = 2; y = 11; copy out x writes i = 2, then y writes i = 11; result 11
With aliasing, reference shares one variable and value-result keeps two copies until the return.

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:

  1. t = x: x is i, so t = 1.
  2. x = y: y is a[i] with i = 1, which is 3. x is i, so i = 3.
  3. y = t: y is a[i] again, but i is 3 now, so the place is a[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.

The three statements of swap with i = 1 and a = [3, 5, 7]. By reference, y is the address @104 fixed at the call: t = x gives t = 1; x = y writes i = 3; y = t writes a[1] = 1; result i = 3, a = [1, 5, 7], correct. By name, y is the thunk a[i]: t = 1; x = y writes i = 3; y = t evaluates a[i] with i = 3 and writes a[3] = 1; result i = 3, a = [3, 5, 1], wrong
By reference fixes a[i] to a[1] at the call; by name evaluates a[i] again after i became 3.

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:

ModeWhat happensResult
by value, reference, value-resultboth arguments are evaluated before the call; next() gives 1, then 1/0 failserror, c = 1
by namex is used three times (x > 0, then x + x), so next() runs three times: 1, then 2 + 3r = 5, c = 3
by neednext() 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

ModeWhere you meet it
by valueC, Java, Python, JavaScript (for a reference to an object, the reference is the value copied)
by referenceC++ int&, Pascal var, C# ref, Fortran
by value-resultAda in out for scalar types, some Fortran compilers
by nameAlgol 60; Scala => T parameters; C macros behave alike (textual)
by needHaskell; 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.

On the mind map

Open the Java & the JVM mind mapJava & the JVMFrom source to bytecode: translate first, or while runningFrom source to bytecodeStartup and class loading: what happens before main()Startup and class loadingJIT compilers: compile hot code using profilesJIT compilersMemory and GC: find and free unreachable objectsMemory and GCCollections: how java.util stores keysCollectionsThreads and memory model: Java threads on real coresThreads and memory modelSpring framework: a container builds your objectsSpring frameworkStack vs heap in JavaStack vs heap in Javaframes on the stack, objects in TLABsException unwindingException unwindingfinally runs; return in finally winsJava GCsJava GCsParallel, G1 regions, ZGC load barriersTracing vs ref countingTracing vs ref countingrefcount frees at once but leaks cyclesPython GCPython GCrefcount plus a cycle collectorGo GCGo GCtri-colour marking with a write barrierPass by value vs referencePass by value vs referenceJava copies the reference, not the objectParameter passing modesBy name re-evaluates a[i] at every useCard table and barriersCard table and barriersold-to-young refs marked dirtySafepointsSafepointsall threads park before a pauseOpen the Java & the JVM mind mapJava & the JVMfind and free unreachable objectsMemory and GCStack vs heap in JavaStack vs heap in Javaframes on the stack, objects in TLABsException unwindingException unwindingfinally runs; return in finally winsJava GCsJava GCsParallel, G1 regions, ZGC load barriersTracing vs ref countingTracing vs ref countingrefcount frees at once but leaks cyclesPython GCPython GCrefcount plus a cycle collectorGo GCGo GCtri-colour marking with a write barrierPass by value vs referencePass by value vs referenceJava copies the reference, not the objectParameter passing modesBy name re-evaluates a[i] at every useCard table and barriersCard table and barriersold-to-young refs marked dirtySafepointsSafepointsall threads park before a pause
This page on the Java & the JVM mind map · 35 pages in 7 branches · open it to see the whole map and test yourself in recall mode.