Tutorials Home   >   Core Programming Concepts   >   Default Parameters

Default Parameters

1. Introduction to Default Parameters

In programming, functions often take parameters (inputs) that control their behavior. Normally, you must provide values for all parameters when calling a function. However, sometimes a parameter has a common or typical value, and you may not always want to provide it.

To solve this, programming languages provide default parameters. A default parameter automatically assumes a value if no value is provided by the caller.


2. What Are Default Parameters?

Definition:

A default parameter is a parameter in a function that is given a default value in the function definition. If the caller does not provide a value, the default is used.

Real-Life Example:

  • A coffee shop offers “small” coffee by default, unless the customer specifies “medium” or “large”.

  • A student record function assigns “class = 10” if the class is not specified.


3. Syntax of Default Parameters (Python Example)

def greet(name="Student"):
print("Hello, " + name)
greet() # Uses default value
greet(“Amit”) # Uses provided value

Output:

Hello, Student
Hello, Amit

Explanation:

  • name="Student" is the default parameter

  • If no argument is passed, "Student" is used


4. Rules for Default Parameters

  1. Default parameters must be placed after non-default parameters.

  2. Non-default parameters are mandatory.

  3. Default parameters are optional.

Correct Example:

def info(name, age=16):
print(name, age)

Incorrect Example:

def info(age=16, name): # ❌ Syntax error
print(name, age)

5. Multiple Default Parameters

A function can have more than one default parameter.

Example:

def student_info(name="Amit", age=16, grade="10th"):
print(name, age, grade)
student_info()
student_info(“Riya”)
student_info(“Riya”, 17)
student_info(“Riya”, 17, “11th”)

Output:

Amit 16 10th
Riya 16 10th
Riya 17 10th
Riya 17 11th

Explanation:

  • Only the parameters not provided take the default value


6. Advantages of Default Parameters

  • Reduce the number of arguments you need to provide

  • Make functions more flexible

  • Avoid repetitive coding

  • Allow common values to be used easily


7. Combining Default and Non-Default Parameters

Default parameters work together with normal parameters, but the order matters.

Example:

def calculate(a, b=5):
return a + b
print(calculate(10)) # 15
print(calculate(10, 7)) # 17
  • b=5 is used if not provided

  • a must always be provided


8. Using Keyword Arguments with Default Parameters

Default parameters can be overridden using keyword arguments.

Example:

def greet(name="Student", msg="Welcome"):
print(msg + ", " + name)
greet()
greet(msg=“Hello”)
greet(name=“Amit”, msg=“Hi”)

Output:

Welcome, Student
Hello, Student
Hi, Amit

Explanation:

  • Keyword arguments allow changing specific default values


9. Mutable Default Parameters (Important Caution)

Default parameters should not be mutable objects like lists or dictionaries, because they can retain changes between function calls.

Problem Example:

def add_item(item, my_list=[]):
my_list.append(item)
return my_list
print(add_item(1)) # [1]
print(add_item(2)) # [1, 2] ❌ unexpected!

Solution: Use None as default and initialize inside the function

def add_item(item, my_list=None):
if my_list is None:
my_list = []
my_list.append(item)
return my_list
print(add_item(1)) # [1]
print(add_item(2)) # [2] ✅ correct

10. Common Uses of Default Parameters

  • Providing optional arguments

  • Setting common default values (like taxes, discounts)

  • Functions with configurable behavior

  • Simplifying function calls


11. Common Mistakes by Beginners

  1. Placing default parameters before non-default ones

  2. Using mutable objects as default values without care

  3. Forgetting that keyword arguments can override defaults

  4. Confusing default parameters with global variables

  5. Passing too many arguments while ignoring defaults


12. Best Practices for Default Parameters

  • Always put default parameters after non-default parameters

  • Avoid mutable objects as defaults unless intentional

  • Use keyword arguments for clarity

  • Keep default values simple and common

  • Comment the default values for readability


13. Default Parameters vs Function Overloading

Some languages (like C++ or Java) allow function overloading to handle multiple argument versions. In Python, default parameters are used instead to simplify multiple cases.


14. Conclusion

Default parameters are a powerful tool in functions that make them flexible and user-friendly. They allow functions to:

  • Use common default values automatically

  • Reduce unnecessary coding

  • Allow optional customization of behavior