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.
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
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 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
Try it yourself
Learn this first
Nothing. This concept starts from scratch.
Learn next
- ReferencesTwo names can lead to one object, so a change through one shows through the other. Beginner
- Stack vs heap memoryFrames that come and go with each call, and blocks you request and must give back. Intermediate
- Pure functionsFunctions that only turn inputs into an output, and why that makes code easy to trust. Beginner
Spotted a mistake on this page?