×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Python Default Parameters

Last Updated : April 24, 2025

Default Parameters

A default parameter is a value provided in a function declaration that is automatically assigned by the compiler if the caller of the function doesn't provide a value for the parameter with the default value.

Defining Function with Default Parameters

You can define a function that accepts default parameters by assigning default values to the parameters in the function definition. The syntax to define a function with default parameter values is as follows:

def function_name(parameter1=value1, parameter2=value2):
    # function body

Example of Function with Default Parameters

Following is a simple Python example to demonstrate the use of default parameters. We don't have to write 3 Multiply functions, only one function works by using default values for 3rd and 4th parameters.

# A function with default arguments, it can be called with
# 2 arguments or 3 arguments or 4 arguments .

def Multiply(num1, num2, num3=5, num4=10):
    return num1 * num2 * num3 * num4

# Main code
print(Multiply(2, 3))
print(Multiply(2, 3, 4))
print(Multiply(2, 3, 4, 6))

Output

300
240
144

Pointe To Remember

  • Default parameters are different from constant parameters as constant parameters can't be changed whereas default parameters can be overwritten if required.
  • Default parameters are overwritten when the calling function provides values for them. For example, calling of function Multiply(2, 3, 4, 6) overwrites the value of num3 and num4 to 4 and 6 respectively.
  • During calling of function, arguments from calling a function to parameters of the called function are copied from left to right. Therefore, Multiply(2, 3, 4) will assign 2, 3 and 4 to num1, num2, and num3. Therefore, the default value is used for num4 only.

Python Default Parameters Exercise

Select the correct option to complete each statement about default parameters in Python.

  1. In Python, you can define default values for function parameters by specifying the value after the ___ symbol in the function definition.
  2. If a function is called without passing a value for a parameter that has a default value, the function will use the ___ value.
  3. Default parameters must appear ___ in the function definition.

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.