Parameters, Arguments, Defaults, and Keywords
Key Takeaways
- Parameters are names in a function definition; arguments are the actual values supplied during a call.
- Positional arguments bind left to right unless keyword arguments name specific parameters.
- A required positional argument cannot be omitted unless the parameter has a default value.
- Default parameter expressions are created when the function is defined, not every time it is called.
- Python raises TypeError for missing, duplicate, or unexpected function arguments.
Parameters and arguments
A parameter is a name listed in a function definition. An argument is a value supplied by the caller. PCEP questions frequently use ordinary names such as a, b, and c, so do not rely on meaning. Bind the call mechanically.
def power(base, exponent):
return base ** exponent
print(power(2, 3))
Here base and exponent are parameters. The values 2 and 3 are arguments. Because the call is positional, base gets 2 and exponent gets 3.
Positional and keyword binding
Arguments can be passed by position or by keyword. Positional arguments are matched left to right. Keyword arguments name the target parameter explicitly.
def describe(name, score, passed=False):
return name + ':' + str(score) + ':' + str(passed)
print(describe('Mia', 82))
print(describe(score=91, name='Sol', passed=True))
Both calls are valid. The second call changes the written order, but keyword names make the binding clear.
| Call pattern | Valid? | Reason |
|---|---|---|
f(1, 2) | Yes | Positional values bind left to right |
f(a=1, b=2) | Yes | Keywords name parameters |
f(a=1, 2) | No | Positional argument follows keyword argument |
f(1, a=2) | Usually no | Parameter a receives a duplicate value if first parameter is a |
f(extra=1) | No, unless extra is a parameter | Unexpected keyword |
The exact error type for these argument-binding failures is usually TypeError. PCEP may ask whether code runs or which exception class best describes the problem.
Defaults
A parameter with a default value becomes optional for the caller. Required parameters must come before parameters with defaults in the function definition.
def total(price, tax=0.10, discount=0):
return price + price * tax - discount
print(total(100))
print(total(100, discount=5))
The first call uses both defaults. The second call uses the default tax and overrides discount by keyword.
Default expressions are evaluated when the def statement runs. At PCEP level, the safest practical rule is: use simple immutable defaults such as numbers, strings, booleans, or None. Mutable defaults such as lists can retain changes between calls, which is a common Python surprise even if the entry-level exam focuses more on recognition than design.
Defaults also do not make earlier required parameters optional. If def f(a, b=2) is called as f(), Python still needs a and raises TypeError. If it is called as f(3), only b is filled from the default.
Matching calls by hand
Use a binding table when a call looks crowded.
def mix(a, b=10, c=20):
return a + b * c
print(mix(2, c=3))
| Parameter | Bound value |
|---|---|
a | 2 |
b | 10 |
c | 3 |
The expression is 2 + 10 * 3, so the result is 32.
Common TypeError patterns
Watch for these traps:
- Calling a function with too few required arguments.
- Calling it with too many positional arguments.
- Passing the same parameter twice, once positionally and once by keyword.
- Using a keyword name that the function does not define.
- Placing a positional argument after a keyword argument.
When an exam snippet fails before the function body starts, argument binding is often the reason. Trace the call header before tracing the body.
Which statement best describes the difference between parameters and arguments?
For def f(a, b=4, c=5): return a + b + c, what is f(1, c=2)?
Which call most directly causes a TypeError because the same parameter is given two values?