Javaexercise.com

Default Arguments in Python Functions

Python is a high-level, interpreted programming language, and is one of the most popular programming languages present in the software industry. One of the most important concepts in python is that of default arguments used in functions. Let us have a look at it in detail.

What are default arguments in Python?

In python, arguments are used in functions to pass on specific information when they are called so that their functionality can be tailored according to requirements.

Arguments are often termed as parameters as well. One type of argument is default arguments.

These are used when you want the function to consider some default values for arguments for which values are not specified during calls.

If the values for these arguments are specified then those values are considered.

Syntax

The syntax to specify default arguments and their default values in python using functions is pretty straightforward and is as follows.

When defining the function, we specify the name of the argument and the corresponding default value separated by an assignment operator.

Do keep in mind the indentations required to specify the function body.  

def <function_name>(<argument_name> = <default_value>):
    <statements>

This is a very simple syntax however there are some rules one needs to keep in mind when specifying default arguments.

The order of arguments in python is very important both while defining and calling functions and you cannot have non-default arguments after default arguments in the signature of the function definition.

Let's understand with some running examples.

Using default arguments in Python Function

In this example, we look at the usage of the concept of default arguments in a function that helps us to calculate the sum of two given arguments.

One unusual thing here is that the arguments have default values as well, in case the values for these arguments are not passed during the function call.

Here we have a function named getSum() that takes in two arguments a and b.

It stores the sum of these arguments in a variable named sum and returns it.

We call this function first using two variables x and y containing values 3 and 2 respectively.

As expected the result is stored in a variable z and on printing it we see an output of 5 which is the sum of 3 and 2.

Let us take a look at the python code and corresponding output for this method:

# Function with default arguments with value 0 for both
def getSum(a = 0, b = 0):
    # Variable to store the sum
    sum = a + b
    
    # Returning the sum
    return sum

# Variables to store values to be passed
x = 3
y = 2

# Calling function
z = getSum(x, y)

# Display result
print(z)

Output

5

Now let us say we pass only one argument to the function, here x corresponding to a, and don’t pass the other argument.

The default value of the other argument b, which is 0, is considered for this function call and we receive 3 as the output on printing the returned value after storing it in a variable named z.

Let us take a look at the python code and corresponding output for this method:

# Function with default arguments with value 0 for both
def getSum(a = 0, b = 0):
    # Variable to store the sum
    sum = a + b
    
    # Returning the sum
    return sum

# Assigning value to only single Variable
x = 3

# Calling function
z = getSum(x)

# Display result
print(z)

Output

3

Similarly, if we pass no parameters, the default value of both the arguments, which is 0, is considered for this function call and we receive 0 as the output on printing the returned value after storing it in a variable named z.

Let us take a look at the python code and corresponding output for this method:

# Function with default arguments with value 0 for both
def getSum(a = 0, b = 0):
    # Variable to store the sum
    sum = a + b
    
    # Returning the sum
    return sum

# Calling function without arguments
z = getSum()

# Display result
print(z)

Output

0

Important rules for default arguments in Python

As mentioned earlier we cannot have default arguments before non-default arguments in a function signature definition.

In this example, we have the same function getSum() as before to return the sum of two arguments but a difference here is that only a is a default argument whereas b is a non-default argument.

As we know, having default arguments before non-default ones are not allowed in python and we get an error stating the same.

Let us look at the python code and corresponding output for this example:

# Function with default arguments with value 0 for one argument only
def getSum(a=0,b):
    # Variable to store the sum
    sum = a + b
    
    # Returning the sum
    return sum

x = 3
y = 2

# Calling function without arguments
z = getSum(x,y)

# Display result
print(z)

Output

SyntaxError: non-default argument follows default argument

To avoid this error, either we can set a default value for both the arguments or make the last argument as a default argument. See, the below example where It works fine.

# Function with default arguments with value 0 for one argument only
def getSum(a,b=0):
    # Variable to store the sum
    sum = a + b
    
    # Returning the sum
    return sum

x = 3
y = 2

# Calling function without arguments
z = getSum(x,y)

# Display result
print(z)

Output:

5

Conclusion

In this topic, we have learned the use and advantages of default arguments in a Python program, following some simple running examples, thus giving us an intuition of how this concept could be applied in real-world situations. Feel free to reach out to info.javaexercise@gmail.com in case of any suggestions.

 

Related Articles: