Parameters and Arguments
Concepts covered: Flexible Arguments
In the header def describe_trip(miles, minutes), the names miles and minutes are parameters: variables the body uses for values that do not exist yet. In the call describe_trip(8, 25), the values 8 and 25 are arguments: the data that fills those variables for this one run of the body. A parameter is a slot; an argument is what goes into it.
def describe_trip(miles, minutes):
print("Miles:", miles)
print("Minutes:", minutes)
describe_trip(8, 25)| Call | miles holds | minutes holds | Prints |
|---|---|---|---|
| describe_trip(8, 25) | 8 | 25 | Miles: 8 then Minutes: 25 |
| describe_trip(1, 6) | 1 | 6 | Miles: 1 then Minutes: 6 |
| describe_trip(40, 90) | 40 | 90 | Miles: 40 then Minutes: 90 |
Positional arguments are matched by order
With no other information, Python matches arguments to parameters by position: the first argument goes into the first parameter, the second into the second. Python checks nothing about meaning, so swapping the 2 numbers in the call swaps what the function believes about the trip:
def describe_trip(miles, minutes):
print("Miles:", miles, "| Minutes:", minutes)
describe_trip(8, 25)
describe_trip(25, 8)Output:
Miles: 8 | Minutes: 25 Miles: 25 | Minutes: 8
The second call describes a 25 mile, 8 minute trip, which no car in a city can drive, and Python reports it without complaint. The order of arguments is the caller's responsibility.
Keyword arguments are matched by name
Writing parameter=value in a call names the slot, so the position no longer matters. Both calls below produce the same line, and the second one mixes the 2 styles:
def describe_trip(miles, minutes):
print("Miles:", miles, "| Minutes:", minutes)
describe_trip(minutes=25, miles=8)
describe_trip(8, minutes=25)Output:
Miles: 8 | Minutes: 25 Miles: 8 | Minutes: 25
One rule governs mixing: every positional argument comes before every keyword argument. The call describe_trip(miles=8, 25) is refused before the program starts, with the message positional argument follows keyword argument, because Python has no way to know which slot the bare 25 belongs in. Keyword arguments earn their keystrokes when a function takes several numbers of the same type: describe_trip(miles=8, minutes=25) reads as a sentence, while describe_trip(8, 25) asks the reader to remember the header.
The count has to match
A function with 2 parameters needs exactly 2 arguments. Supplying fewer leaves a slot empty, and Python stops the call before the body runs:
def describe_trip(miles, minutes):
print("Miles:", miles, "| Minutes:", minutes)
describe_trip(8)Running it raises TypeError: describe_trip() missing 1 required positional argument: 'minutes'. The message names the function and the empty slot. Supplying too many arguments fails the same way, with describe_trip() takes 2 positional arguments but 3 were given. Either way the body never starts, so a print inside it leaves no trace in the output.
Every call fills the slots again
A call inside a loop runs the body once per pass, and each pass delivers new arguments. Surge pricing multiplies a base fare by a level from 1 to 3; the loop below passes the same cents each time and a different multiplier, and the final call fills both slots by keyword:
def print_surge_fare(cents, multiplier):
total = cents * multiplier
print("Base", cents, "x", multiplier, "=", total)
for level in range(1, 4):
print_surge_fare(1000, level)
print_surge_fare(multiplier=2, cents=635)Base 1000 x 1 = 1000 Base 1000 x 2 = 2000 Base 1000 x 3 = 3000 Base 635 x 2 = 1270
On each pass multiplier takes the loop's current value, and total is computed fresh from that pass's arguments. The keyword call at the end lists multiplier before cents, and the output shows that the order in the call made no difference.
What does the second call print?
def label(first, second):
print(second, first)
label(2, 5)
label(second=2, first=5)- 2 5, the body printing second first
- 5 2, matching the first call
- 5 2 and 2 5 on one line
- A TypeError for out-of-order keywords
When a program runs without errors but reports a trip that cannot exist, check each call against the header, slot by slot. Line up the first argument with the first parameter and the second with the second, and ask whether each value makes sense in that slot. A swapped pair of numbers is the bug Python never reports, because 25 is as valid a mile count as 8. The fix is to swap them back, or to name the slots with keywords.
About this lesson
This section is part of the Functions: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.