Professor Turing

Study guides

What is the difference between pass by value and pass by reference?

Last updated: 2026-11-26

With pass by value, assigning to a parameter leaves the caller's variable alone, while with pass by reference it changes that variable. Python hands the function a reference to the same object, so a change made inside that object is visible to the caller, but rebinding the parameter name is not.

Start learning No account needed

1. Two ideas from the textbooks, and what Python does

In pass by value, the function receives the value of the argument, often as a copy. Assigning to the parameter changes only the function's local variable. In pass by reference, the parameter stands for the caller's variable itself, so assigning to it changes what the caller sees.

Python fits neither short label neatly. Its FAQ says that arguments are passed by assignment, and that there is no call by reference. The tutorial calls it "call by value" and explains that the value handed over is a reference to an object, not a copy of the object. A footnote there prefers the name "call by object reference". Wikipedia's name for this arrangement is call by sharing: the function and the caller hold the same object, but each has its own name for it. So two different actions inside a function have different effects. Rebinding points the local name at something else. Mutating changes the shared object in place.

2. A worked example: five calls

Predict each printed line before you read the table.

def add_one(count):
    count = count + 1

def replace(account):
    account = dict(balance=0)

def deposit(account):
    account.update(balance=account.get("balance") + 30)

def swap(a, b):
    a, b = b, a

def same_object(account):
    return account is savings

visits = 7
savings = dict(balance=50)
left, right = "tea", "milk"

print("same object inside the call:", same_object(savings))
add_one(visits)
print("after add_one:", visits)
replace(savings)
print("after replace:", savings)
deposit(dict(savings))
print("after deposit on a copy:", savings)
deposit(savings)
print("after deposit:", savings)
swap(left, right)
print("after swap:", left, right)
CallWhat the function does with the parameterWhat the caller sees afterwards
add_one(visits)gives the local name a new number7, unchanged
replace(savings)gives the local name a new dictionarybalance 50, unchanged
deposit(dict(savings))changes a copy of the dictionarybalance 50, unchanged
deposit(savings)changes the shared dictionarybalance 80
swap(left, right)exchanges two local namestea milk, unchanged

Output from the script scripts/pass-by-value-vs-pass-by-reference-worked-example.py, which runs the code above, simulates pass by reference, checks how deep a copy goes and checks the self-test answers:

same object inside the call: True
after add_one: 7
after replace: {'balance': 50}
after deposit on a copy: {'balance': 50}
after deposit: {'balance': 80}
after swap: tea milk
simulated pass by reference, after replace: {'balance': 0}
set stored inside a copied dictionary is still shared: True
question 1, basket holds: apple, pear
question 2, total and returned value: 10 15
checks passed

The first line shows that inside the call, the parameter and the caller's savings are the very same object. replace then rebinds its local name, so the caller's dictionary stays at 50. deposit calls a method that changes the object, so the caller sees 80. Under pass by reference, replace would have left the caller with balance 0. The script shows that case with a small simulation, because Python has no call by reference to run.

3. A common mistake: "objects are passed by reference"

After seeing deposit change the caller's dictionary, it is tempting to say Python passes objects by reference. replace and swap are the counterexamples. If the parameter stood for the caller's variable, both would change what the caller sees, and neither does. The other tempting summary, "Python passes copies", fails on deposit(savings): the caller's dictionary changed, so the function was not working on a copy. A copy exists when the code makes one, as in dict(savings), and that copies the outer dictionary alone. An object stored inside it is still shared, as the script's output shows.

The question that predicts the result is what the line inside the function does to the object. An assignment to the parameter name rebinds it. A method that edits the object, such as update on a dictionary or add on a set, mutates it. Numbers, strings and tuples cannot be changed in place, so a function cannot alter the caller's number or string this way. Watch +=: the Python FAQ notes that it creates a new object for an integer or a tuple but mutates a list.

When you want a new value back, return it and assign it at the call, as in visits = new_count(visits). The FAQ recommends returning the results as the clearest approach in nearly all cases. Other languages set their own rules, so check the one your course uses.

4. Test yourself

Write your prediction before you open the answer.

Question 1. A caller has basket, a set holding apple. It calls a function that runs items.add("pear"), then one that runs items = set(), each with basket as the argument. What does the basket hold?

Answer to question 1

apple and pear. The first function mutates the shared set. The second rebinds its own local name and leaves the caller's set alone.

Question 2. total is 10. A function runs n += 5 on its parameter and returns n. After result = bump(total), what are total and result?

Answer to question 2

total is 10 and result is 15. An integer cannot be changed in place, so += made a new number and bound the local name to it.

Then explain, without the page, why swap prints the old order. For a study plan, see how to study for a computer science exam and how to test yourself.

What the sources found, and their limits

Python documentation, Programming FAQ and tutorial. The FAQ gives passing by assignment, the absence of call by reference, the behavior of += on lists, integers and tuples, and the advice to return results. The tutorial gives the description of arguments as object references passed by value. Sources: Programming FAQ and More Control Flow Tools, read on 2026-11-25.

Wikipedia, Evaluation strategy. It gives the definitions of call by value, call by reference and call by sharing, and names Python among languages that use call by sharing. Sources: Evaluation strategy, read on 2026-11-25.

Limits: the example is ours and covers Python. The names for these strategies vary between authors and language communities, so use your course's terms in an exam and say what the code does.

How Professor Turing helps

Professor Turing is a tutor for adults learning any subject, in college or on their own. It asks you questions instead of giving the final answer, does not grade a bare guess, and remembers what you got wrong. No account is needed to start.

Bring a function whose output you predicted wrong. Its first reply asks what you already know before it teaches anything. If you answer with a bare guess, it asks how you got there instead of saying right or wrong, and when you stay stuck it changes its approach and makes the next step smaller instead of giving the final answer. You must be 18 or older, and guest conversations are deleted 7 days after the last activity.

FAQ

Is Python pass by value or pass by reference? Python's FAQ says arguments are passed by assignment and that there is no call by reference. The function gets a reference to the same object, which some sources call call by sharing: mutations show up in the caller, and rebinding the parameter does not.

Why did my function change the list I passed in? The function changed the object itself, for example with append or with += on a list, and the caller's name points to that same object. Pass a copy if the function should not affect the original.

Start with what you're working on

Tell the tutor which function call you cannot predict and let it ask you the questions.

Start learning No account needed

See also: how to study for a computer science exam · what is a pointer in programming · how to test yourself · all study guides