Python Function Arguments
Function arguments are the real values passed to a function in a call. Parameters are the names in the function definition.
One parameter
Here name is the parameter. The text Ada is the argument.
def greet(name):
print(f"Hello, {name}!")
greet("Ada")Order matters
The first argument goes to the first parameter. The second goes to the second.
def show_pair(first, second):
print(first, second)
show_pair("top", "bottom")
show_pair("bottom", "top")Too few arguments
This function needs one argument. Calling it with none raises an error.
def greet(name):
print(f"Hello, {name}!")
greet()Any value works
An argument can be a number, text, or any other value. The parameter simply holds what you pass.
def show(value):
print(value)
show(42)
show("cake")Example
def describe_pet(name, kind):
print(f"{name} is a {kind}.")
describe_pet("Milo", "cat")
describe_pet("Ruby", "dog")Key points
- Parameter: the name in the function definition, like a placeholder
- Argument: the real value you pass when you call the function
- Positional argument: an argument matched to a parameter by its position
- Use commas to separate parameters and to separate arguments
In this example, name and kind are the parameters. The first call passes Milo and cat as arguments. Milo goes to name, and cat goes to kind. The block prints one line about Milo. The second call passes Ruby and dog. The block prints one line about Ruby.
Note
Python matches positional arguments from left to right.
Swapping the order swaps which parameter gets each value.
Try it Yourself
Edit the code, then press Run.
Running is simulated · Python Playground
Exercise
In greet("Ada"), what is Ada?