Default Parameters in Python Functions

By | December 2, 2024

A default parameter is a value provided in function declaration that is automatically assigned by the compiler if caller of the function doesn’t provide a value for the parameter with default value. Following is a simple Python example to demonstrate 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

# Driver code
if __name__ == "__main__" :

    print( Multiply( 2 , 3 ) )
    print( Multiply( 2 , 3 , 4 ))
    print( Multiply( 2 , 3 , 4 , 6))

Output: 

300
240
144

Key Points:

  1. Default parameters are different from constant parameters as constant parameters can’t be changed whereas default parameters can be overwritten if required.
  2. Default parameters are overwritten when 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.
  3. During calling of function, arguments from calling function to parameters of 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, default value is used for num4 only.
Author: Mithlesh Upadhyay

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.