Lesson 22 of 30 · Functions
Parameters and Arguments
Pass information into Python functions. Learn parameters, arguments, default values and keyword arguments, with clear runnable examples.
- 6 min read
- Beginner
- Reviewed
- How to pass information into a function
- The difference between a parameter and an argument
- How to give a parameter a default value
- How to pass arguments by name
Functions that adapt
The functions in the last lesson did the same thing every time. That is limiting. print() would be useless if it could only print one fixed message.
A parameter is a variable in the function's brackets that receives a value each time the function is called.
def greet(name):
print(f"Hello, {name}!")
greet("Ada")
greet("Grace")
greet("Alan")One function, three different results. Each call puts a different value into name.
Parameter or argument?
The two words are often mixed up. The difference is small but useful.
- A parameter is the name in the definition:
nameindef greet(name): - An argument is the actual value in the call:
"Ada"ingreet("Ada")
Think of the parameter as an empty box with a label, and the argument as what you put in it.
More than one parameter
Separate parameters with commas. Arguments are matched to parameters by position: first to first, second to second.
def describe_pet(animal, name):
print(f"I have a {animal} called {name}.")
describe_pet("dog", "Rex")
describe_pet("cat", "Luna")Swap the arguments and the sentence comes out wrong, because Python cannot know what you meant.
def describe_pet(animal, name):
print(f"I have a {animal} called {name}.")
describe_pet("Rex", "dog")Arguments can be any value
You can pass variables, calculations and collections, not only values typed directly.
def show_total(prices):
print("Items:", len(prices))
print("Total:", sum(prices))
basket = [30, 10, 120]
show_total(basket)
show_total([5, 5])Default values
Give a parameter a default value with = in the definition. The caller can then leave that argument out.
def greet(name, greeting="Hello"):
print(f"{greeting}, {name}!")
greet("Ada")
greet("Grace", "Good morning")Parameters with defaults must come after the ones without.
Keyword arguments
You can name the arguments in the call. These are called keyword arguments. Their order no longer matters, and the call explains itself.
def describe_pet(animal, name):
print(f"I have a {animal} called {name}.")
describe_pet(name="Rex", animal="dog")You have already used one: scores.sort(reverse=True).
Common mistakes
Write a greet function with a name parameter and call it twice.
# Define a function called greet that takes one parameter, name, # and prints: Hello, <name>! # Then call it for Ada and for Grace. Output must be exactly: # Hello, Ada! # Hello, Grace!
Given def power(base, exponent=2): what does power(5) use for exponent?
Summary
- Parameters are the names in a function's definition. Arguments are the values in a call.
- Arguments match parameters by position, unless you name them.
- A default value makes an argument optional.
- Keyword arguments such as
name="Ada"make calls clearer.
Next: getting a result back out of a function.
Finished reading? Tick it off to track your progress.