Chapter 5: Functions / Lesson 28

Default & Keyword Arguments

Default Arguments

Default arguments allow you to specify default values for function parameters. If a parameter isn't provided when calling the function, it uses the default value. This makes functions more flexible and easier to use.

Default arguments simplify function calls by allowing optional parameters with sensible defaults, reducing the need to pass the same values repeatedly.

Basic Default Arguments

You can assign default values to parameters by using the assignment operator in the function definition:

default_args.py
# Function with default argument def greet(name, message="Hello"): print(f"{message}, {name}!") greet("Alice") # Output: Hello, Alice! greet("Bob", "Hi") # Output: Hi, Bob! # Function with multiple default arguments def power(base, exponent=2): return base ** exponent print(power(5)) # Output: 25 (5²) print(power(5, 3)) # Output: 125 (5³)

Multiple Default Arguments

Functions can have multiple parameters with default values. Parameters with defaults must come after parameters without defaults:

multiple_defaults.py
# Function with multiple default arguments def create_profile(name, age=18, city="Unknown"): print(f"Name: {name}, Age: {age}, City: {city}") create_profile("Alice") # Uses both defaults create_profile("Bob", 25) # Uses city default create_profile("Charlie", 30, "NYC") # All specified # Common pattern: greeting function def greet(name, greeting="Hello", punctuation="!"): print(f"{greeting}, {name}{punctuation}") greet("Alice") # Hello, Alice! greet("Bob", "Hi") # Hi, Bob!

⚠️ Important Rule

Parameters without defaults must come before parameters with defaults. You cannot have a parameter with a default value followed by one without.

Keyword Arguments

Keyword arguments allow you to specify which parameter you're passing by name, rather than by position. This makes function calls more readable and allows you to skip optional parameters:

keyword_args.py
# Function with multiple parameters def calculate(amount, tax_rate=0.1, discount=0): total = amount * (1 + tax_rate) - discount return total # Positional arguments (by position) result1 = calculate(100, 0.15, 10) # Keyword arguments (by name) result2 = calculate(amount=100, tax_rate=0.15, discount=10) # Mix of positional and keyword result3 = calculate(100, discount=10) # Keyword arguments allow skipping parameters result4 = calculate(amount=100, discount=5) # Uses default tax_rate print(result1, result2, result3, result4)

Benefits of Keyword Arguments

Keyword arguments provide several advantages:

  • Readability - Makes function calls self-documenting
  • Flexibility - Allows skipping optional parameters
  • Order Independence - Can pass arguments in any order when using keywords
  • Clarity - Makes code easier to understand and maintain

Practical Examples

Here are practical examples combining default and keyword arguments:

examples.py
# Timer function with defaults def countdown(start, end=0, step=1): for i in range(start, end - 1, -step): print(i) print("Done!") countdown(5) # Counts from 5 to 0 countdown(10, step=2) # Counts by 2s # Format message with defaults def format_message(text, prefix="INFO", suffix=""): print(f"[{prefix}] {text} {suffix}") format_message("Processing complete") format_message("Error occurred", prefix="ERROR")

Best Practices

✅ When to Use Default Arguments

• For optional parameters with sensible defaults

• To reduce repetitive function calls

• For configuration parameters that rarely change

• To make functions backward compatible

💡 Important Notes

• Default values are evaluated once when the function is defined

• Parameters without defaults must come before those with defaults

• Keyword arguments improve code readability

• You can mix positional and keyword arguments (positional first)

🎉

Lesson Complete!

Great work! Continue to the next lesson.

main.py
📤 Output
Click "Run" to execute...