A function without parameters is like a machine with a fixed setting — it always does the exact same thing. Parameters are the way you pass information into a function so it can do something useful with different inputs each time you call it. They transform a function from a single-use snippet into a truly reusable tool.
Parameters vs Arguments
These two words are often confused, but they mean different things:
- A parameter is the variable name listed in the function definition.
- An argument is the actual value you pass when you call the function.
def greet(name): # 'name' is the parameter
print(f"Hello, {name}!")
greet("Alice") # "Alice" is the argument
greet("Bob") # "Bob" is the argument
Here name is a parameter in the definition. "Alice" and "Bob" are arguments provided when the function is called.
Think of a parameter as an empty box in the function definition. When you call the function with an argument, the value is placed in that box for the duration of the call.
Multiple Parameters
Functions can have more than one parameter. Separate them with commas:
def calculate_area(width, height):
area = width * height
print(f"Area: {area} cm²")
calculate_area(5, 3) # width=5, height=3
calculate_area(10, 4) # width=10, height=4
When you call a function with multiple arguments, Python matches them to parameters by position — the first argument goes to the first parameter, the second to the second, and so on.
Default Parameter Values
You can give parameters a default value that is used when no argument is supplied:
def power(base, exponent=2):
result = base ** exponent
print(result)
power(5) # uses default exponent=2, prints 25
power(5, 3) # uses exponent=3, prints 125
Default values are great for optional settings. Put parameters with defaults at the end of the parameter list — Python requires this.
Keyword Arguments
When calling a function you can name the arguments explicitly. This makes the code more readable and lets you pass them in any order:
def describe_pet(animal, name):
print(f"{name} is a {animal}.")
describe_pet(name="Fluffy", animal="cat")
A Common Mistake: Too Few or Too Many Arguments
def add(a, b):
print(a + b)
add(3) # TypeError — missing one argument
add(3, 4, 5) # TypeError — too many arguments
Python will immediately raise a TypeError if the number of arguments does not match the number of parameters (taking defaults into account). Always count your parameters and arguments carefully.
Putting It Together
def make_email(username, domain="school.edu"):
email = username + "@" + domain
print(email)
make_email("alice") # [email protected]
make_email("bob", "gmail.com") # [email protected]
make_email(username="carol") # [email protected]
Parameters make make_email flexible enough to generate either a school address or any custom domain with the same function.
Practice Problems
In def multiply(x, y): return x * y — when you call multiply(4, 6) — which are the parameters and which are the arguments?
Show answer
What is the output of this code? def show(a, b=10): print(a + b) show(5) show(5, 3)