Default Parameters In Python Functions

Functions with Default Parameters in Python, provide a programmer a means to specify default values for one or parameters of a function definition.

During the function invocation, the default parameters need not be provided any value.

However, other values can as well be passed if a value other than the default value needs to be provided.

Remember, unlike some of the programming languages, Python does not provide function overloading.

However, functions with default parameters give an easy way around to implement multiple algorithms inside the same function.

Default Parameters in Python Functions - Example:

import math

 

# method with default parameter - "Binary" is the value for the default parameter

def GCD(m,n,GCD_Algorithm="Binary"):

    #implementation for the default parameter

    # GCD is found using binary method

    if(GCD_Algorithm=="Binary"):

        q = 1

        p = 0

        while m%2 == 0 and n%2 == 0:

            m = m/2

            n = n/2

            q = 2*q

 

        while m > 0:

            if m %2 == 0:

                m = m/2

            elif n%2==0:

                n = n/2

            else:

                p = math.trunc(abs(m-n)/2)

   

                if m < n:

                    n = p

                else:

                    m = p

 

        return n*q

 

    # implementation for the custom parameter value - "Euclid"

    # GCD is found using Euclid method

    elif(GCD_Algorithm == "Euclid"):

        if (n == 0):

            return m

        elif(m >= n and n > 0):

            return GCD(n, (m % n),"Euclid")

        else:

            print("Invalid Input;Consider reordering such that X > Y")

   

#Invoke the function using the default parameter value(Binary GCD Method)

a = 512

b = 48  

aGCD = GCD(a,b)

print("Using default Parameter->GCD of two numbers {} and {} is: {}".format(a,b,aGCD))

 

#Invoke the function using the custom parameter value  - Recursive Euclid algorithm

aGCD = GCD(512,48,"Euclid")

print("Using custom Parameter->GCD of two numbers {} and {} is: {}".format(a,b,aGCD))

Output:

Using default Parameter->GCD of two numbers 512 and 48 is: 16

Using custom Parameter->GCD of two numbers 512 and 48 is: 16

The example python program implements a function GCD with a default parameter. 

The GCD function takes a parameter called GCD_Algorithm, which is initialised with a default value "Binary".

Invocation of the GCD function can be done simply by calling

GCD(512, 48)

or the default paramter value can be specified explicitly 

GCD(512, 48,"Binary") #default parameter specified explicitly

#or

GCD(512, 48,GCD_Algorithm="Binary")

All the above invocations using default paramters are valid in Python and will result in the same output.

Passing Custom value for a Python function with a default parameter: 

In the above example, a custom value can be passed for the default parameter GCD_Algorithm with either of the two ways given below:
 

GCD(512, 48,"Euclid") #passing custom value to a default parameter

#or

GCD(512, 48,GCD_Algorithm="Euclid")

 


Copyright 2023 © pythontic.com