Okay, you've learned how to define a basic function using the def
keyword. This bundles code, but functions become truly powerful when you can pass information into them. Imagine a function that greets a user; it needs to know the user's name. Or a function that adds two numbers; it needs to know which numbers to add. This is where parameters and arguments come in.
Let's look at a simple example:
# Define a function with one parameter named 'user_name'
def greet(user_name):
"""Prints a simple greeting."""
print(f"Hello, {user_name}! Welcome.")
# Call the function, passing the argument "Alice"
greet("Alice")
# Call the function again with a different argument
greet("Bob")
In this code:
user_name
inside def greet(user_name):
is the parameter. It's a placeholder within the function for the name we want to greet."Alice"
and "Bob"
are the arguments. These are the specific values we provide when we call the greet
function. When greet("Alice")
runs, the value "Alice"
is assigned to the user_name
parameter inside the function.Functions can accept multiple parameters, separated by commas in the definition. When you call the function, you provide the corresponding arguments in the same order. This is called using positional arguments because the position determines which parameter receives which argument.
def describe_pet(animal_type, pet_name):
"""Displays information about a pet."""
print(f"I have a {animal_type}.")
print(f"My {animal_type}'s name is {pet_name}.")
# Call with positional arguments
describe_pet("hamster", "Harry")
print("---") # Just to separate outputs
describe_pet("dog", "Willie")
Output:
I have a hamster.
My hamster's name is Harry.
---
I have a dog.
My dog's name is Willie.
Here, in the first call describe_pet("hamster", "Harry")
:
"hamster"
(the first argument) is assigned to animal_type
(the first parameter)."Harry"
(the second argument) is assigned to pet_name
(the second parameter).The order matters. If you called describe_pet("Max", "cat")
, the output would be incorrect because "Max"
would be assigned to animal_type
and "cat"
to pet_name
.
Mapping of positional arguments to parameters. The first argument goes to the first parameter, the second to the second, and so on.
Python also allows you to use keyword arguments. This involves specifying the parameter name followed by an equals sign (=
) and the value you want to pass.
def describe_pet(animal_type, pet_name):
"""Displays information about a pet."""
print(f"I have a {animal_type}.")
print(f"My {animal_type}'s name is {pet_name}.")
# Call using keyword arguments
describe_pet(animal_type="hamster", pet_name="Harry")
print("---")
# Order doesn't matter with keyword arguments
describe_pet(pet_name="Willie", animal_type="dog")
Output:
I have a hamster.
My hamster's name is Harry.
---
I have a dog.
My dog's name is Willie.
When using keyword arguments:
pet_name="Willie"
).Mapping of keyword arguments to parameters. Arguments are matched to parameters based on the specified name, regardless of order.
You can mix positional and keyword arguments when calling a function. However, there's a rule: positional arguments must come before keyword arguments.
def describe_pet(animal_type, pet_name):
"""Displays information about a pet."""
print(f"I have a {animal_type}.")
print(f"My {animal_type}'s name is {pet_name}.")
# Valid: Positional argument first
describe_pet("cat", pet_name="Whiskers")
# Invalid: Keyword argument cannot be followed by a positional one
# describe_pet(animal_type="dog", "Buddy") # This would cause a SyntaxError
Understanding how to pass data into functions using parameters and arguments is fundamental to creating flexible and reusable code. You can now write functions that operate on different data each time they are called.
© 2025 ApX Machine Learning