Python Program to Use Default Arguments in a Function

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

  1. Define the Function: Use the def keyword to define a function named greet_user.
  2. Add a Parameter with a Default Value: Assign a default value of "Guest" to the name parameter.
  3. Add Function Logic: Inside the function, print a greeting message that includes the name.
  4. Call the Function without an Argument: Call the function without providing the name argument to use the default value.
  5. Call the Function with an Argument: Call the function with a specific name argument 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_user is defined using the def keyword.
  • The parameter name is given a default value of "Guest". This means that if the name argument 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 the name parameter. 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 the name.

Step 4: Call the Function without an Argument

  • The function is called using greet_user() without passing any arguments. Since no name is 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.

Leave a Comment

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

Scroll to Top