Parameters and Arguments
Functions often need specific data to work with. This data is passed into the function using parameters.
- Parameters: The names defined in the function definition (inside the parentheses).
- Arguments: The actual values passed to the function when it is called.
Defining Functions with Parameters
python def welcome(name, age): # 'name' and 'age' are parameters print(f"Welcome, {name}! You are {age} years old.")
Positional Arguments
The arguments are matched to parameters based on their position.
python welcome('Bob', 25) # 'Bob' maps to 'name', 25 maps to 'age'
Output: Welcome, Bob! You are 25 years old.
Keyword Arguments
You can specify arguments using the parameter names when calling the function. This improves clarity and allows you to pass arguments out of order.
python welcome(age=40, name='Cathy') # Order doesn't matter here