► Overview
Parameters and arguments allow functions to accept inputs, making them flexible and reusable. Parameters are variables in the function definition, while arguments are the actual values passed when calling the function.
► Real-World Analogy
Think of ordering at a restaurant:
» Menu item (parameter): “Pizza”
» Your choice (argument): “Margherita, Large, Extra Cheese”
» Same menu, different customizations each time
► Key Concepts
✓ Parameters: Variables defined in function signature
✓ Arguments: Actual values passed to function
✓ Positional arguments: Order matters
✓ Keyword arguments: Use parameter names
✓ Default parameters: Provide fallback values
✓ *args and **kwargs: Variable number of arguments
■ Parameters Examples
/code
Positional parameters
def greet(name, age):
print(f”Hello {name}, you are {age} years old”)
greet(“Alice”, 25)
Default parameters
def power(base, exponent=2):
return base ** exponent
print(power(5)) # Uses default exponent=2
print(power(5, 3)) # Override with 3
Keyword arguments
def describe_pet(animal, name, age):
print(f”{name} is a {age} year old {animal}”)
describe_pet(name=”Buddy”, animal=”dog”, age=3)
Output:
Hello Alice, you are 25 years old
25
125
Buddy is a 3 year old dog
► Important Points
✓ Parameters make functions flexible and reusable
✓ Use default parameters for optional values
✓ Keyword arguments improve code readability
✓ Positional arguments must come before keyword arguments
✓ *args for variable positional, **kwargs for variable keyword
✓ Choose descriptive parameter names
► Remember
✓ Parameters are in function definition
✓ Arguments are values you pass when calling
✓ Order matters for positional arguments
✓ Default values prevent errors for missing arguments
✓ Use keyword arguments for clarity
■ Practice Challenge
- Create a function with 3 parameters, one with default value
- Write a function that accepts both positional and keyword arguments
- Make a function that calculates area with width and height parameters
