Understanding Default Arguments in Python Functions

Today we’re going to talk about one of my favorite features in this beautiful language: default arguments.

To start, let’s define what a default argument is and how it works.In Python, when defining a function with multiple arguments, you can assign certain values as “defaults” for those arguments. This means that if someone calls your function without passing in a value for one or more of the parameters, they will automatically get assigned their respective defaults.

Here’s an example:

# Defining a function with multiple arguments and assigning default values
def greet(name="John Doe", message="Hello"): # Function name and parameters with default values assigned
    print(f"{message}, {name}!") # Prints a message with the given name and message, using f-strings for formatting

# Example of calling the function with default values
greet() # Output: Hello, John Doe!

# Example of calling the function with custom values
greet("Jane", "Hi") # Output: Hi, Jane!

# Example of calling the function with only one argument
greet(name="Bob") # Output: Hello, Bob!

# Example of calling the function with keyword arguments
greet(message="Hey", name="Sally") # Output: Hey, Sally!

In this case, we have two arguments in our function `greet()`. The first one is called `name`, and it has a default value of “John Doe”. This means that if someone calls the function without passing in any argument for `name`, they will automatically get assigned “John Doe” as their name.

The second argument, on the other hand, is also optional but doesn’t have a default value. If you call this function with only one argument (let’s say `greet(“Alice”)`), then Alice will be printed out without any message because we didn’t pass in anything for the `message` parameter.

But what if we want to change that default value? Let’s say we have a function called `calculate_total()`, and it takes two arguments: `price` and `tax`. By default, tax is set at 10%. But let’s say we want to create another version of this function where the tax rate can be customized. We could do that like so:

# Define a function called calculate_total with two parameters: price and tax
def calculate_total(price, tax=None):
    # Check if the tax parameter is set to None (no value provided)
    if tax == None:
        # If tax is None, set it to 0.1 (default value for tax is 10%)
        tax = 0.1
    # Calculate the total by multiplying the price by 1 plus the tax rate
    total = price * (1 + tax)
    # Return the total value
    return total

In this example, we’ve added an optional argument called `tax`. By default, it has a value of `None`, which means that if someone calls the function without passing in anything for `tax`, they will get assigned 0.1 as their tax rate (which is equivalent to 10%).

But what happens when we pass in our own customized tax rate? Let’s say we want to calculate a total with a tax rate of 25%. We could do that like so:

# This script calculates the total price of an item with a given tax rate and prints the result.

# Define the function "calculate_total" with two parameters: price and tax.
def calculate_total(price, tax):
    # If no tax rate is passed in, assign a default value of 0.1 (10%).
    if tax is None:
        tax = 0.1
    # Calculate the total price by adding the price and the price multiplied by the tax rate.
    total = price + (price * tax)
    # Return the total price.
    return total

# Call the function "calculate_total" with a price of $3.99 and a tax rate of 0.25 (25%).
total = calculate_total(price=3.99, tax=0.25)
# Print the total price, including the tax rate, using string formatting.
print(f"Total price (including {tax*100}% tax): ${total}")

In this case, we’re passing in our own customized value for `tax`, which is 0.25 or 25%. The function will calculate the total price based on that new tax rate and print it out to us.

Default arguments are a powerful tool in Python that can save you time, make your code more readable, and help you avoid ***** errors caused by missing parameters. Give them a try next time you’re writing some functions, and let me know how they work for you!

SICORPS