Module 9 · Functions — topic 4 of 19
Parameters
The names in a function's header that receive argument values; how parameters work as local variables, how mutable arguments can be changed, and parameter kinds in a signature.
Learning objectives
- Distinguish parameters from arguments
- Explain why rebinding a parameter doesn't affect the caller
- Predict when mutating a parameter does affect the caller
Definition
A parameter is a variable named in a function's definition. When the function is called, each parameter is bound to the corresponding argument value and behaves as a local variable inside the body.
Introduction
Parameters are the placeholders in a function definition. In def area(width, height), width and height are parameters; in area(3, 4), the numbers 3 and 4 are arguments that fill them.
Why it matters
Python passes references to objects. Knowing what that means stops surprises such as a function that unexpectedly empties a list you passed in.
Detailed explanation
Parameters are created fresh for every call and disappear when the call ends. They are local: code outside the function can't see them.
Python binds each parameter to the object the caller passed — no copy is made. Assigning a new value to the parameter (x = 5) only rebinds the local name; the caller's variable is unaffected.
If the object is mutable (a list, dict or set) and the function changes it in place (append, update), the caller sees the change, because both names refer to the same object.
A function's signature lists its parameters, including defaults, *args and **kwargs, and optionally keyword-only or positional-only markers (* and /).
Choose parameter names that describe the value, not its type: price, not number1.
Syntax
- Parameters live only during the call.
- Rebinding a parameter doesn't affect the caller.
- Mutating a mutable argument does.
Code examples
Rebinding doesn't leak out
10
Mutating does
[75, 85]
More examples
Protecting the caller's data
[0.0, 0.33, 1.0] [10, 20, 40] [0.0, 0.33, 1.0]
Step-by-step explanation
- normalised builds and returns a new list, so data is unchanged afterwards.
- normalise_in_place assigns to the slice values[:], which replaces the contents of the same list object.
- Because the caller's list is that same object, data now holds the scaled values.
- Name functions so readers can tell which style they use.
Expected output
Running “Protecting the caller's data” prints exactly:
Practical applications
- Sorting functions come in both styles: sorted(list) returns new, list.sort() changes in place.
- APIs document whether they modify their arguments.
Best practices
- Avoid modifying arguments unless that's the function's purpose.
- Name functions that mutate clearly (normalise_in_place, add_to).
- Keep the number of parameters small — usually five or fewer.
Common mistakes
Expecting rebinding to update the caller
Avoid
Prefer
Return the new value and assign it in the caller.
Accidentally changing an argument
Avoid
Prefer
sort() reorders the caller's list; sorted() works on a copy.
Important notes
- This model is sometimes called 'pass by object reference' or 'pass by assignment'.
Key takeaways
- Parameters are local names bound to the arguments' objects.
- Rebinding a parameter never affects the caller.
- Mutating a mutable argument does.
Try it yourself
- 1Write remove_negatives(values) in two versions: one returning a new list, one modifying in place.
- 2Explain why def f(s): s += "!" doesn't change a string passed in, but def g(l): l += [1] changes a list.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
15 questions cover the “Functions” module. Answers and explanations are revealed after you submit.
- BeginnerWhat is printed?
- BeginnerWhat is printed?
- BeginnerWhat is printed?