What a Function Can Change

Parameter Passing determines how a function receives information and which changes can remain visible to its caller. You meet it whenever a function updates a number, edits a list, or attempts to replace an object. Keep track of the argument, the parameter, and any object they access. Similar-looking assignments can affect different things.

Values and caller variables

With pass by value, the parameter is a separate variable initialized with the argument's value. Assigning another value to that parameter changes the local variable. It does not, by itself, assign anything to the caller's variable.


With pass by reference, the parameter designates the caller's variable. An assignment through the parameter therefore updates that variable. Think of the parameter as another route to the same storage slot.


The governing distinction at the start of a call is:


Value passing: p←value⁡(x)Reference passing: location⁡(p)=location⁡(x)\begin{aligned} \text{Value passing: }&p \gets \operatorname{value}(x)\\ \text{Reference passing: }&\operatorname{location}(p)=\operatorname{location}(x) \end{aligned}


Here, a location means a variable's storage slot. Copying a value into a new slot differs from giving a parameter access to an existing slot.


Example: Suppose the caller's integer score is 16 and the function assigns 21 to its parameter. Value passing leaves score at 16. Reference passing changes score to 21. This comparison assumes the assignment is the function's only operation affecting that value.

Shared objects

A value can itself be a reference value. Copying this value does not necessarily copy the object. Two separate variables can consequently access the same object. Such names are aliases.


Python's object-argument behavior is often called pass by sharing. A function can change a mutable object, such as a list, through its local parameter. Assigning a different object to that parameter changes which object the local name refers to.


A caller name and a parameter name point to one shared list object.


In the diagram, p[0] = 10 changes the shared list, so the caller can observe [10, 12]. By contrast, p = [0, 0] makes p refer to another list. The caller's items name continues to refer to the original list.



Java passes all arguments by value, including reference values for arrays and other objects. An array parameter can modify shared array elements, but assigning a new array to that parameter does not replace the caller's array variable. Sharing describes the resulting object access; it does not make Java pass variables by reference.


Passing behavior

Parameter setup

Effect of assigning a replacement to the parameter

Value passing

Separate variable receives the argument's value

Caller variable is unchanged

Reference passing

Parameter designates the caller's variable

Caller variable receives the replacement

Sharing an object

Separate local name refers to the original object

Caller name still refers to the original object


Trace the target of each assignment

First identify what the parameter receives: an ordinary value, access to a caller variable, or access to a shared object. Then classify each statement by its target.


For a Python list parameter, p[0] = 10 changes an element of the object currently reached through p. The statement p = [0, 0] changes the local name's association. After that assignment, later element changes through p affect the replacement list.


Track the current object after every reassignment. An earlier shared mutation remains in the original object even if the parameter subsequently refers elsewhere.



What to remember

  • Value passing creates a separate parameter variable; reference passing provides access to the caller's variable.
  • A copied reference value can still provide access to shared mutable contents.
  • Changing an object's contents and assigning a replacement to a local parameter have different effects.
  • Trace statements in order and identify the target of every change.

Example: Updating an Inventory List

Example: A stock-checking script stores the numbers of items in two bins in a Python list. Another caller name, backup, is assigned that same list; no list copy is made. Trace the call below. Every list entry and the variable take represents a count of items.


Python
def adjust(items, amount):
    items[0] = items[0] - amount
    amount = amount + 1
    items = [0, 0]
    items[1] = 99

stock = [14, 9]
backup = stock
take = 4
adjust(stock, take)


a) What does stock contain immediately after the first statement inside adjust?


At entry, items refers to the same list as stock. The initial first-bin count is ________, and the quantity being subtracted is ________. The assignment changes an element of that existing list. This is a mutation, a change to an object's contents.


new first-bin count=14−4=10 items\text{new first-bin count} = 14 - 4 = 10\ \text{items}


The second element is not assigned, so it remains 9 items. Immediately after the first statement, stock contains [10, 9].


b) What is the local amount immediately after the second statement, and what is the caller's take after the call finishes?


The local name amount initially refers to the integer value 4. The second statement calculates:


new local amount=4+1=5 items\text{new local amount} = 4 + 1 = 5\ \text{items}


Assigning this result to amount is a reassignment: the local name now refers to the new value. It does not assign a new value to the separate caller name take. Integers are immutable, so this arithmetic does not change the integer object originally supplied.


The local amount is 5 items after the second statement. The caller's take remains 4 items after the call.


c) Immediately before the function finishes, what list does local items refer to? After the call, what lists do stock and backup refer to?


The statement items = [0, 0] creates a new list and redirects only the local name items. Its first entry is initially ________, and its second entry is initially ________. The final statement replaces the second entry of this new list with 99 items, leaving its first entry unchanged.


The earlier subtraction already changed the original list to [10, 9]. Redirecting items does not reverse that change. Also, both caller names still refer to the original list, because neither stock nor backup receives a new assignment.


Immediately before the function finishes, local items refers to [0, 99]. After the call, both stock and backup refer to the same list containing [10, 9]. A name called backup provides no independent copy unless the program actually creates one.

The following pseudocode passes its integer argument by value. The symbol := means assignment. Which explanation correctly predicts the caller's count after the procedure finishes?


pseudocode
procedure addThree(p):
    p := p + 3

count := 11
addThree(count)
Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A parcel-pricing program declares a function with the header deliveryFee(mass). Inside another function named checkout, the program executes deliveryFee(parcelMass). Match each term to its role in a function call.

Match each term
A.

An expression supplied in a particular function call.

B.

The function that invokes another function.

C.

The function invoked by another function.

D.

A name declared in a function header to receive a supplied input.

Callee

Caller

Formal parameter

Actual argument

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

In this pseudocode, both parameters are passed by reference, and := means assignment. The same caller variable is supplied twice. What is the final value of n? Enter a unitless integer, to zero decimal places.


pseudocode
procedure change(a, b):
    a := a + 4
    b := b * 3

n := 6
change(n, n)

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice