5.2 Arguments and Parameters
We explore how to pass data to functions through parameters and arguments, making our code dynamic and adaptable.
Functions become truly powerful when we can pass data to them for processing. This is done through parameters and arguments.
- Parameters: These are the variables declared within the parentheses in the function definition. They act as placeholders for the values the function will receive.
- Arguments: These are the actual values you provide to the function when you call it.
Positional and Keyword Arguments
Positional Arguments: Python matches the values you pass with the parameters based on their order. The first argument goes to the first parameter, the second to the second, and so on.
Keyword Arguments: You can explicitly specify which parameter each value corresponds to by using the parameter's name (e.g., `age=30`). This makes the order of the arguments irrelevant and the code much more readable, especially in functions with many parameters.
# Function with multiple parameters
def display_info(name, age, city):
"""Displays a person's information."""
print(f"Name: {name}, Age: {age}, City: {city}")
# Calling with positional arguments
display_info("George", 25, "Athens")
# Calling with keyword arguments (order doesn't matter)
display_info(age=30, city="Thessaloniki", name="Eleni")
Explore More with AI
Use AI to generate new examples, delve deeper into theory, or get your questions answered.