Python Function with Default Parameter Values


Function with Default Parameter Values in Python

In Python, you can define functions with default parameter values. This means that if a value for a parameter is not provided when the function is called, the function will use the default value instead. This feature makes your functions more flexible and easier to use.

Defining Default Parameter Values

You can specify default values for parameters in the function definition by assigning a value to the parameter in the function signature.

Syntax

def function_name(parameter1=default_value1, parameter2=default_value2, ...): # Code block (function body) return value # Optional

Example of a Function with Default Parameter Values

Here's a simple example of a function that greets a person. If no name is provided, it defaults to "Guest":

def greet(name="Guest"): """This function greets the person with the given name or defaults to 'Guest'.""" print(f"Hello, {name}!") # Calling the function with and without an argument greet() # Uses the default value greet("Alice") # Uses the provided value

Output:

Hello, Guest! Hello, Alice!

Breakdown of the Example

  1. Function Definition:

    • The function greet is defined with one parameter, name, which has a default value of "Guest".
    • If no argument is provided during the function call, the function will greet "Guest".
  2. Function Call:

    • When greet() is called without any arguments, the default value "Guest" is used, resulting in the output "Hello, Guest!".
    • When greet("Alice") is called, the argument "Alice" is passed, and the output is "Hello, Alice!".

Multiple Default Parameters

You can define multiple parameters with default values. Parameters with default values should be placed after any parameters without default values in the function signature.

def introduce(name, age=18, city="Unknown"): """This function introduces a person with their name, age, and city.""" print(f"My name is {name}. I am {age} years old and I live in {city}.") # Calling the function with different combinations of arguments introduce("Alice") # Uses default values for age and city introduce("Bob", 25) # Uses default value for city introduce("Charlie", 30, "New York") # Uses provided values

Output:

My name is Alice. I am 18 years old and I live in Unknown. My name is Bob. I am 25 years old and I live in Unknown. My name is Charlie. I am 30 years old and I live in New York.

Keyword Arguments with Default Values

You can also use keyword arguments to specify which parameters to set when calling the function. This allows you to skip parameters with default values while still providing specific values for others.

introduce(age=22, name="Eve") # Skipping the city parameter

Output:

My name is Eve. I am 22 years old and I live in Unknown.

Summary

  • Functions can have default parameter values, which are used if no argument is provided during the function call.
  • Default parameters make functions more flexible and easier to use by allowing some arguments to be optional.
  • When defining multiple parameters, parameters without default values should be listed before those with default values.
  • You can use keyword arguments to specify which parameters to set, allowing you to skip parameters with default values.

Functions with default parameter values enhance the usability of your code and enable you to create more versatile functions that can accommodate different input scenarios.