Programming languages / Pass by value vs pass by reference

Pass by value vs pass by reference

What a function actually receives when you call it: a copy, an alias, or a copy of an arrow. Seven steps follow one small call and what the caller sees afterwards.

Beginner7 steps~6 min
Variable — a named box that holds a value
Reference — an arrow from a box to an object
Object — lives apart from the variables
// THE SHORT VERSION

In 30 seconds

Pass by value gives a function a copy of the argument. The function can change its copy freely; the caller’s variable stays as it was.

Pass by reference gives the function the caller’s own variable under a second name, so an assignment inside the function changes the caller’s variable.

Java, Python and JavaScript pass by value, but for objects the value being copied is a reference: an arrow to the object. The function can change the shared object, yet it cannot make the caller’s variable point somewhere else.

Pass by value vs pass by reference, step by step

STEPS01 / 07

Simplified: Python passes numbers the same way as objects, but numbers cannot be changed in place, so the result matches a copy. Passing a reference by value is also called call by sharing.

STEP 01 / 07 One call, two names
THE MATH
let a = 5 bump(a) n = ?
  1. Step 1: One call, two names. The caller owns a variable a that holds 5. The function bump calls its input n. When the program runs bump(a), something has to connect a to n. A language can do it in two ways: hand over a copy of the value, or hand over the caller’s own variable.
  2. Step 2: By value: copy the 5. With pass by value, the call copies what is inside a into a fresh box named n. Now there are two boxes that both happen to hold 5. Nothing links them: n belongs to the function and disappears when the function returns.
  3. Step 3: The copy changes, a does not. Inside the function, n = n + 1 writes 6 into n’s box. The caller’s box was never involved, so a is still 5 and the program prints 5. With pass by value a function cannot change the caller’s variable, whatever it does to its parameter.
  4. Step 4: By reference: one box. C++ can pass by reference: the & makes n an alias, a second name for the caller’s box, with no box of its own. Now n = n + 1 writes straight into a, and the caller sees 6. C# offers the same with the ref keyword.
  5. Step 5: Objects: copy the arrow. In Java, Python and JavaScript a variable does not contain an object. It holds a reference: an arrow to the object, which lives elsewhere in memory. Passing cart copies the arrow, not the array. This is still pass by value; the value is a reference.
  6. Step 6: Change the object: seen. c.push("tea") follows c’s arrow and changes the array at the end of it. The caller’s arrow leads to that same array, so the caller sees the new item. This is why people say objects are passed by reference. The next step shows why that is not quite right.
  7. Step 7: Reassign the name: not seen. Assigning to c points the function’s own arrow at a new array. The caller’s arrow is a separate copy and does not move, so the program prints the old array. Real pass by reference would have replaced the caller’s array too. That is the test that tells the two apart.
VALUES SO FAR
a5caller
n····by value
a after····by value
a after····by ref
c····by value
cart····mutated
cart····unchanged
// BEFORE

Learn this first

Nothing. This concept starts from scratch.

Spotted a mistake on this page?