Python Functions
Estimated reading: 3 minutes 25 views

โš™๏ธ Python Default Arguments โ€“ Complete Guide with Examples

๐Ÿงฒ Introduction โ€“ Why Use Default Arguments?

In Python, functions are powerful because they offer flexibilityโ€”and default arguments make them even more adaptable.

With default arguments, you can assign a default value to parameters. This lets users call your function with fewer arguments while still giving them the option to override those defaults.

Default arguments are essential when designing APIs, utilities, or any code that benefits from optional parameters.

๐ŸŽฏ In this guide, you’ll learn:

  • How default arguments work in Python
  • The order and rules of default parameters
  • Real-world use cases
  • Best practices and common pitfalls

๐Ÿ”ง Syntax of Default Arguments

def function_name(param1=value1, param2=value2):
    # function body
  • Parameters with default values must come after non-default parameters.

โœ… Example 1: Function with One Default Parameter

def greet(name="Guest"):
    print(f"Hello, {name}!")
greet()           # Output: Hello, Guest!
greet("Alice")    # Output: Hello, Alice!

๐Ÿ“˜ Explanation:
If no argument is passed, "Guest" is used by default.


๐Ÿงฎ Example 2: Multiple Default Parameters

def power(base, exponent=2):
    return base ** exponent

print(power(3))       # 9
print(power(3, 3))    # 27

๐Ÿ’ก Use Case:
Let users compute square by default, or choose their own exponent.


โŒ Example 3: Incorrect Parameter Order

# โŒ This will raise a SyntaxError
# def example(a=1, b): 
#     pass

โš ๏ธ Warning:
Default arguments must come after all required (non-default) ones.


๐Ÿ“˜ Real-World Example: Sending Emails with Optional Subject

def send_email(to, subject="No Subject", body=""):
    print(f"To: {to}")
    print(f"Subject: {subject}")
    print(f"Body: {body}")

send_email("user@example.com")

Output:

To: user@example.com
Subject: No Subject
Body:

โœ… Tip: Default arguments are ideal for optional parameters like subject, headers, or timeout.


๐Ÿ” Combining Positional and Default Arguments

def book_flight(source, destination="New York"):
    print(f"Booking flight from {source} to {destination}")

book_flight("London")               # destination defaults to New York
book_flight("London", "Chicago")   # override default

๐Ÿ” Summary โ€“ Key Takeaways

  • โœ… Use default arguments to make parameters optional
  • ๐Ÿ” Default values are assigned only if no argument is passed
  • โš ๏ธ Always put default parameters after non-default ones
  • ๐Ÿ“ฆ Great for setting fallback values and simplifying function calls

โ“ FAQ Section

โ“ Can default arguments be functions or objects?

Yes, but mutable defaults (like lists or dicts) should be avoided directly. Use None and set defaults inside the function.

โ“ Can I override default arguments?

Yes! Just pass a value in the function call.

โ“ Are default arguments stored in memory?

Yes, the default values are evaluated once at function definition time.

โ“ Can I mix default and non-default arguments?

Yes, but all non-default arguments must come first.

โ“ How are default arguments different from *args and **kwargs?

  • default arguments give fallback values.
  • *args accepts variable non-keyword arguments.
  • **kwargs accepts variable keyword arguments.

Share Now :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

Python Default Arguments

Or Copy Link

CONTENTS
Scroll to Top