Sub topic 2
Parameters and arguments
This chapter assumes you can define a function with def, call it, and use basic data types such as strings, lists and numbers. It separates two terms that people often mix up, and it shows what Python does when the two don't match.
Two words, two roles
A parameter is a name in a function's definition. It stands for a value the function will receive. An argument is the actual value you pass when you call the function.
def greet(name): # name is a parameter
return f"Hello, {name}!"
message = greet("Ada") # "Ada" is an argument
print(message) # Hello, Ada!Each call binds the argument to the parameter, so inside the function name refers to "Ada". A different call binds a different value, and the function body stays the same.
Several parameters
A function can take any number of parameters, separated by commas. By default, Python matches arguments to parameters in order.
def power(base, exponent):
return base ** exponent
print(power(2, 10)) # 1024
print(power(10, 2)) # 100Swapping the arguments changes the result, because the first argument goes to base and the second to exponent. Later chapters cover keyword arguments, which let you pass values by name, and default values, which make a parameter optional.
When the counts don't match
Python checks the call before it runs the body. If you pass too few or too many arguments, it raises a TypeError and the function does not execute.
def power(base, exponent):
return base ** exponent
try:
power(2)
except TypeError as error:
print(error)
# power() missing 1 required positional argument: 'exponent'
try:
power(2, 3, 4)
except TypeError as error:
print(error)
# power() takes 2 positional arguments but 3 were givenThe messages name the function and the problem, so read them closely. "Missing" means you passed too few arguments. "Takes 2 but 3 were given" means you passed too many.
Parameters are local names
A parameter exists only while the function runs. It is a new variable created for each call, separate from any variable with the same name outside the function.
def double(number):
number = number * 2
return number
number = 5
print(double(number)) # 10
print(number) # 5The outer number is still 5. The function rebinds its own number and leaves the caller's variable alone. A later chapter covers local scope, and another covers what happens when the argument is a mutable object such as a list.
Parameters are not types
A parameter name says nothing about what kind of value it accepts. Python does not check types when you call a function, so a function works with any argument that supports the operations in its body.
def double(value):
return value * 2
print(double(4)) # 8
print(double("ab")) # abab
print(double([1, 2])) # [1, 2, 1, 2]The same code works for numbers, strings and lists because * is defined for each of them. A value that doesn't support the operation fails at that line, for example double(None) raises a TypeError. Type hints, covered later, document the intended types without enforcing them at call time.
Choosing good parameter names
Parameter names are part of how people read your function, and callers can use them as keyword names later. Pick names that describe the role of the value.
- Prefer
temperature_covert. - Use nouns for data and avoid names that repeat the function name.
- Avoid shadowing built-ins such as
list,strormax.
Exercise
In result = power(3, 4) with def power(base, exponent): ..., which names are parameters and which values are arguments? What happens if you call power(3)?
Show answer
base and exponent are parameters. 3 and 4 are arguments. base is bound to 3 and exponent to 4, so the result is 81. Calling power(3) raises a TypeError that says exponent is a missing required positional argument, and the function body does not run.