Default Arguments in Python Functions

Functions in Python are used to implement logic that you want to execute repeatedly at different places in your code. You can pass data to these functions via function arguments. In addition to passing arguments to functions via a function call, you can also set default argument values in Python functions. These default values are assigned to function arguments if you do not explicitly pass a parameter value to the given argument. Parameters are the values actually passed to function arguments.

In this article, you will see how to use default arguments in Python functions. But first, we will see how to define a function in Python and how to explicitly pass values to function arguments.

Function without Arguments

Let's define a very simple Python function without any arguments:

def my_function():
    print("This is a function without arguments")

The above script defines a function, my_function, which doesn't accept any arguments and simply prints a string.

The following script shows how you'd actually call the my_function() function:

my_function()

In the output, you should see a simple statement printed to the screen by the my_function() function:

This is a function without arguments

Function with Explicit Arguments

Let's now define a simple Python function where we have to pass multiple values for the function arguments. If you do not specify values for all the function arguments, you will see an error.

Here is the function we'll be using as an example:

def func_args(integer1, integer2):
    result = integer1 + integer2
    return result

In the code above we create a function, func_args(), with two arguments integer1 and integer2. The function adds the values passed in the two arguments and returns the result to the function caller.

Let's try calling the above function with two arguments:

result = func_args(10, 20)
print(result)

The above script calls the func_args() method with two parameter values, i.e. 10 and 20. In the output, you should see the sum of these two values, i.e. 30.

Let's now try to call the func_args() method without passing values for the arguments:

result = func_args()
print(result)

In the output, you should see the following error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-13-3449c8e5e188> in <module>
----> 1 result = func_args()
      2 print(result)

TypeError: func_args() missing 2 required positional arguments: 'integer1' and 'integer2'

The error is quite clear, the function call to func_args() is missing the 2 required positional arguments, integer1 and integer2. The error basically tells us that we need to pass values for the integer1 and integer2 arguments via the function call.

Let's now pass a value for one of the arguments and see what happens:

result = func_args(10)
print(result)

Now in the output, you should again see the following error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-14-640ec7b786e1> in <module>
----> 1 result = func_args(10)
      2 print(result)

TypeError: func_args() missing 1 required positional argument: 'integer2'

The difference here is that the error now tells us that the value for one of the positional arguments, i.e. integer2, is missing. This means that without any default argument values set, you have to pass values explicitly for all the function arguments, otherwise an error will be thrown.

What if you want your function to execute with or without the argument values in the function call? This is where default arguments in Python functions come in to play.

Function with Default Arguments

Default arguments in Python functions are those arguments that take default values if no explicit values are passed to these arguments from the function call. Let's define a function with one default argument.

def find_square(integer1=2):
    result = integer1 * integer1
    return result

The above script defines a function find_square() with one default argument i.e. integer1. The default value for the integer1 argument is set to 2. If you call the find_square() method with a value for the integer1 argument, the find_square() function will return the square of that value.

Otherwise, if you do not pass any value for the integer1 argument of the find_square() function, you will see that the default value, i.e. 2, will be assigned to integer1, and the function will return the square of 2, i.e. 4.

Let's first call the find_square() method with the argument value of 10:

result = find_square(10)
print(result)

Output:

100

When you execute the above script, the value 10 overwrites the default value of 2 for the argument integer1 of the find_square() function and the function returns 100, which is square of 10.

Now we will call the find_square() function without any value for the argument1 argument. In this case, you will see that 4 will be returned by find_square() function since in the absence of the value for the find_square() function, the default value of 2 will be used as the value for the find_square() function, as shown below:

result = find_square()
print(result)

Output:

4

A Python function can have multiple default arguments as well. For instance, in the following script, the function adds the integers number passed to the arguments. If none of the integer values is passed to the function, the default arguments would take values 2 and 4 respectively, as shown below:

def add_ints(integer1=2, integer2=4):
    result = integer1 + integer2
    return result

Let's first call the add_ints() function without any parameters:

result = add_ints()
print(result)

Output:

6

Since we did not pass any values for the function arguments, the default argument values, i.e 2 and 4, have been added together.

Let's now pass two of our own values to the add_ints() function:

result = add_ints(4, 8)
print(result)

Output:

12

As expected, 4 and 8 were added together to return 12.

A Python function can have both normal (explicit) and default arguments at the same time. Let's create a function take_power(). The first argument to the function is a normal argument while the second argument is a default argument with a value of 2. The function returns the result of the value in the first argument raised to the power of value in the second argument.

def take_power(integer1, integer2=2):
    result = 1
    for i in range(integer2):
        result = result * integer1

    return result

Let's first pass only a single argument:

result = take_power(3)
print(result)

Output:

9

In the script above, 3 has been passed as a value to the integer1 argument of the take_power() function. No value has been provided for the default argument integer2. Hence, the default value of 2 will be used to take the power of 3 and you will see 9 in the output.

Let's now pass two values to the take_power() function.

result = take_power(3, 4)
print(result)

In the output, you will see 3 raised to the fourth power, i.e. 81.

It's important to note that parameters with default arguments cannot be followed by parameters with no default argument. Take the following function for example:

def take_power(integer1=2, integer2):
    result = 1
    for i in range(integer2):
        result = result * integer1

    return result

Trying to call this function will result in an error since the first argument has a default, but the second one does not:

result = take_power(3, 4)
print(result)

Executing this code results in the following error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-14-640ec7b786e1> in <module>
----> 1 def take_power(integer1=3, integer2):
      2     result = 1

SyntaxError: non-default argument follows default argument