Python > Core Python Basics > Functions > Calling Functions
Function Call with Default Argument Values
This snippet demonstrates how to define a function with default argument values and how to call it with or without providing those arguments. It highlights the flexibility of using default values to simplify function calls.
Code Example
The power
function calculates base
raised to the power of exponent
. The exponent
parameter has a default value of 2. When calling the function with only the base
argument, the exponent
defaults to 2, effectively calculating the square. When both arguments are provided, the function calculates the base
raised to the specified exponent
.
def power(base, exponent=2):
"""Calculates the power of a number. If no exponent is provided, it defaults to 2 (square)."""
result = base ** exponent
return result
# Calling the function with only the base (exponent defaults to 2)
square = power(5)
print(f"The square of 5 is: {square}")
# Calling the function with both the base and the exponent
cube = power(5, 3)
print(f"The cube of 5 is: {cube}")
Concepts Behind the Snippet
Default argument values allow you to define a function that can be called with fewer arguments than defined. If an argument is not provided during the function call, the default value is used. This makes the function more versatile and easier to use in different situations.
Real-Life Use Case
Consider a function to create a user profile. You might have default values for certain fields like 'country' or 'language', but allow the user to override these defaults if they choose to.
Best Practices
Place parameters with default values at the end of the parameter list. This ensures that positional arguments (those without keywords) are always provided first. Use meaningful default values that make sense in most scenarios. Always document the default values in the function's docstring.
Interview Tip
Be prepared to discuss the order of arguments in a function definition with default values. Understand that arguments without default values must come before arguments with default values.
When to use them
Use default argument values when you want to provide a reasonable default for a parameter that might not always be needed. This simplifies the function call for common use cases while still allowing for customization.
Memory footprint
Default arguments don't significantly impact memory footprint. The memory overhead is similar to calling a function without default arguments. The default value is stored once when the function is defined, not each time the function is called.
Alternatives
Instead of default arguments, you could use conditional logic inside the function to handle cases where an argument is missing. However, default arguments are often more concise and readable.
Pros
Default arguments simplify function calls for common use cases, making the function more flexible and user-friendly. They also reduce code duplication by providing a default behavior without requiring the caller to explicitly specify it.
Cons
If the default value is a mutable object (like a list or dictionary), it can lead to unexpected behavior if the function modifies it. In such cases, it's recommended to use None
as the default value and create a new mutable object inside the function if needed.
FAQ
-
Can I override the default argument value when calling the function?
Yes, you can override the default value by explicitly providing a value for that argument in the function call. -
What happens if I have multiple arguments with default values and I only want to override one of them?
You can use keyword arguments to specify which argument you want to override. For example:power(base=5, exponent=4)
.