Introduction
Default arguments in Python allow you to define a function with default values for one or more parameters. This feature is useful when you want to make some parameters optional or provide a default behavior if no argument is provided for those parameters. This tutorial will guide you through creating a Python program that uses default arguments in a function.
Example:
- Function Name:
greet_user - Function Purpose: Print a greeting message, with a default name if none is provided.
- Program Output:
Hello, Guest! Hello, Ramesh!
Problem Statement
Create a Python program that:
- Defines a function named
greet_user. - The function takes one argument,
name, with a default value of"Guest". - The function prints a greeting message using the provided name or the default name.
- Calls the function both with and without the argument to demonstrate the use of default arguments.
Solution Steps
- Define the Function: Use the
defkeyword to define a function namedgreet_user. - Add a Parameter with a Default Value: Assign a default value of
"Guest"to thenameparameter. - Add Function Logic: Inside the function, print a greeting message that includes the
name. - Call the Function without an Argument: Call the function without providing the
nameargument to use the default value. - Call the Function with an Argument: Call the function with a specific
nameargument to override the default value.
Python Program
# Python Program to Use Default Arguments in a Function
# Author: https://www.rameshfadatare.com/
# Step 1: Define the function with a default argument
def greet_user(name="Guest"):
# Step 3: Add function logic
print(f"Hello, {name}!")
# Step 4: Call the function without an argument (uses default value)
greet_user()
# Step 5: Call the function with an argument (overrides default value)
greet_user("Ramesh")
Explanation
Step 1: Define the Function with a Default Argument
- The function
greet_useris defined using thedefkeyword. - The parameter
nameis given a default value of"Guest". This means that if thenameargument is not provided when the function is called, it will use"Guest"as the value.
Step 2: Add a Parameter with a Default Value
- The
name="Guest"in the function definition assigns a default value to thenameparameter. If a value is provided when the function is called, it will override this default.
Step 3: Add Function Logic
- Inside the function, the
print()function is used to display a greeting message that includes thename.
Step 4: Call the Function without an Argument
- The function is called using
greet_user()without passing any arguments. Since nonameis provided, the default value"Guest"is used.
Step 5: Call the Function with an Argument
- The function is called using
greet_user("Ramesh"), providing the argument"Ramesh". This overrides the default value, so the greeting will include"Ramesh"instead of"Guest".
Output Example
Example Output:
Hello, Guest!
Hello, Ramesh!
Additional Examples
Example 1: Function with Multiple Default Arguments
# Function to display user information with default arguments
def display_user_info(name="Guest", age=18, country="India"):
print(f"Name: {name}, Age: {age}, Country: {country}")
# Calling the function without any arguments (uses all default values)
display_user_info()
# Calling the function with some arguments (overrides some default values)
display_user_info(name="Ramesh", age=25)
# Calling the function with all arguments (overrides all default values)
display_user_info(name="John", age=30, country="USA")
Output:
Name: Guest, Age: 18, Country: India
Name: Ramesh, Age: 25, Country: India
Name: John, Age: 30, Country: USA
Example 2: Function with a Default Argument in the Middle
# Function to calculate the total cost with a default tax rate
def calculate_total_cost(price, tax_rate=0.05, discount=0):
total = price + (price * tax_rate) - discount
print(f"Total Cost: {total}")
# Calling the function with default tax rate
calculate_total_cost(100)
# Calling the function with a custom tax rate and discount
calculate_total_cost(100, tax_rate=0.1, discount=5)
Output:
Total Cost: 105.0
Total Cost: 105.0
Example 3: Function with No Default Argument
# Function to print a custom message
def print_message(message):
print(message)
# Calling the function with a custom message
print_message("Hello, this is a custom message.")
Output:
Hello, this is a custom message.
Conclusion
This Python program demonstrates how to use default arguments in a function, allowing you to define optional parameters with default values. This feature makes your functions more flexible and easier to use, as you can provide default behavior while still allowing for customization when needed. Understanding how to use default arguments is essential for writing efficient and user-friendly Python functions.