Parameters and arguments
print prints whatever you give it in the parentheses. Your own functions can take values too. Name a variable in the parentheses of the header, and the function gets a value in it every time it's called:
def greet(name):
print(f"Hello, {name}!")
greet("Ada")
greet("Linus")
Hello, Ada!
Hello, Linus!The variable in the header, name, is a parameter. The value in the call, "Ada", is an argument. When the call runs, the parameter gets the argument as its value, and then the body runs. Each call can give a different argument, so one function does the same work with different values.
The two words are easy to mix up, and people often use them loosely, but the difference is worth knowing: a parameter is the name the function uses inside, and an argument is the value a call gives it from outside.
Any value as an argument
An argument can be any expression: a value written out, a variable, or a calculation. Python works out its value first and passes on the result:
def show_square(number):
print(f"{number} squared is {number * number}")
show_square(4)
side = 7
show_square(side)
show_square(side + 1)
4 squared is 16
7 squared is 49
8 squared is 64In the call show_square(side), the parameter number gets the value of side, 7. The names don't have to match: the function only ever sees the value.
Inside the body, a parameter is a variable like any other. You can use it in a calculation, an if, a loop or an f-string, and even change it: the change stays inside the function.
A missing argument
A function with a parameter needs an argument in every call:
def greet(name):
print(f"Hello, {name}!")
greet()
Traceback (most recent call last):
File "main.py", line 5, in <module>
greet()
~~~~~^^
TypeError: greet() missing 1 required positional argument: 'name'The message names the function and the parameter that got no value. "Positional" means that arguments are matched to parameters by their position, first to first. That matters as soon as a function has more than one parameter, in the next step.