Parameters and Arguments
Passing Information into Functions
A function that always works on the same fixed values is rarely useful. To compute the area of any rectangle, or greet any user by name, a function needs a way to receive input each time it is called. Parameters and arguments are that mechanism. You meet them as soon as you write a function that should work for more than one case, and they are behind many of the most common errors in early programs: values in the wrong order, the wrong number of values, and surprises about what a function can change.
Parameters receive, arguments supply
def area(width, height):
return width * height
print(area(3, 5))A parameter is a variable named in the function header; here width and height are parameters. An argument is a value supplied in a call; here 3 and 5 are arguments. When the call runs, Python first evaluates every argument, then binds each value to its parameter before the body starts. The call above behaves as if the body began with two assignments:
width = 3
height = 5Because arguments are evaluated first, an argument can be any expression: area(2 + 1, side) computes 3 and looks up side before the function starts. The names of the caller's variables play no part in matching. Calling area(height, width) with variables that happen to be named height and width still sends the first value to the parameter width.
Inside the body, parameters behave like ordinary local variables that exist only while that call runs.
Matching by position or by name
By default, arguments are positional: they are matched to parameters by position, left to right. The number of positional arguments must fit the header. Calling area(3) raises a TypeError because height receives nothing, and area(1, 2, 3) raises one because there is no third parameter.
Python also lets a call name its parameters, and lets a header give a parameter a fallback value.
Style | Example | How the value is matched |
|---|---|---|
Positional argument | area(3, 5) | By order: first argument to first parameter |
Keyword argument | area(height=5, width=3) | By parameter name, so order is free |
Default value | def greet(name, greeting="Hello") | Used when the call supplies nothing for greeting |
Two ordering rules keep matching unambiguous. In a call, positional arguments come before keyword arguments, so area(width=3, 5) is a syntax error. In a header, parameters with defaults come after those without.
What the function can change
In Python, a parameter receives a reference to the same object the argument refers to. That has two consequences, and students often expect only one of them.
def add_bonus(points, items):
points = points + 10
items.append("bonus")
score = 50
bag = ["key"]
add_bonus(score, bag)
print(score, bag) # 50 ['key', 'bonus']Reassigning a parameter, as in points = points + 10, only rebinds the local name; score in the caller is still 50. Changing a mutable object through the parameter, as items.append does, changes the one list that both items and bag refer to, so the caller sees the change.
What to remember
- Parameters are the names in the header; arguments are the values in the call.
- Arguments are evaluated first, then bound to parameters before the body runs.
- Positional arguments match by order, keyword arguments match by name, and defaults fill parameters the call omits.
- Too few or too many arguments raise a TypeError; swapped arguments of the same type usually do not.
- Reassigning a parameter does not affect the caller, but mutating a shared object does.
Example: Tracing arguments into a price function
A school store prices sale items with the function below. price is in dollars, discount_percent is a percentage, and tax_rate is a decimal fraction. The built-in round(value, 2) rounds the result to cents.
def final_price(price, discount_percent, tax_rate=0.13):
discounted = price * (1 - discount_percent / 100)
return round(discounted * (1 + tax_rate), 2)For each call, state the value each parameter receives, then find what the call returns or explain why it fails.
a) final_price(40, 25)
Both arguments are positional, so they bind left to right: price receives 40 and discount_percent receives 25. The call gives no third argument, so tax_rate takes its default value, 0.13.
The fraction of the price kept is 1−25/100= ________, so discounted is 40×0.75= ________. Adding tax:
The call returns 33.9, so the item costs $33.90.
b) final_price(25, 40)
The numbers are the same as in part a, in the opposite order. Matching is by position, so now price receives 25 and discount_percent receives 40; tax_rate is again 0.13.
The fraction kept is 1−40/100=0.6, so discounted is 25×0.6= ________. Adding tax:
The call returns 16.95. Python raises no error, because both values are valid numbers; the swap simply prices a $25 item at a 40% discount instead of a $40 item at 25%. Only the order of the arguments tells Python which number is the price.
c) final_price(tax_rate=0.05, discount_percent=10, price=200)
Every argument is a keyword argument, so each binds by name and the order in the call does not matter: price receives 200, discount_percent receives 10 and tax_rate receives 0.05, overriding the default.
The fraction kept is 1−10/100=0.9, so discounted is 200×0.9= ________. Adding tax:
The call returns 189.0, a price of $189.00.
d) final_price(120)
The single positional argument binds to the first parameter, so price receives 120. The next parameter, discount_percent, has no default value and receives no argument, so it cannot be bound.
Python raises a TypeError before the body runs, reporting that final_price() is missing 1 required positional argument, discount_percent. Nothing is returned. Calling final_price(120, 0) would supply the missing value and return 135.6.
A shop uses the following Python functions to print item labels. The function show_pair should print its received item name followed by the number 2, regardless of which name it receives.
def show_item(item, count):
print(item, count)
def show_pair(name):
show_item(MISSING_NAME, MISSING_COUNT)The uppercase placeholders represent missing expressions. Complete the helper call using a parameter name and an integer literal: replace MISSING_NAME with and MISSING_COUNT with .
A program uses the Python function below to count the floor tiles needed for a set of rows, with a few spare tiles if requested.
def tile_count(rows, cols=4, spare=0):
return rows * cols + spareFor each call in the table, enter the value bound to each parameter and the value the call returns. Enter whole numbers only.
| Call | rows | cols | spare | Returned value |
| tile_count(5) | ||||
| tile_count(3, 6) | ||||
| tile_count(cols=2, rows=7) | ||||
| tile_count(4, spare=3) |