In the Python programming language, functions are used to implement logic that you need to execute multiple times at different locations in your code. A function accepts data from a function call via arguments. Functions arguments can have default values that are used in case the function calls do not explicitly pass values for function arguments. The default values that a function uses to initialize arguments are called default arguments. These default arguments can be used to implement function overloading as we’ll explain in this tutorial. Function overloading occurs when a function with the same name behaves differently when a different number of arguments is passed to it.

Before we see examples of functions with default arguments, let’s first review Python functions with no arguments and functions with non-default arguments.

Functions with No Arguments

A function without arguments doesn’t require input when calling the function in order for it to work. Here’s an example:

def function1():
    print("A simple function with no arguments")

The above script creates a simple function named function1(). The function accepts no arguments and simply prints a string on the output console when called. Here’s the function call to the function1() function.

function1()

You can see that the call to function1(), doesn’t contain any values for the function arguments. Here is the output of the script above:

A simple function with no arguments

Function with Non-Default arguments

A function can have non-default arguments. For such a function, you’re required to pass the values for the function arguments via the function call. Here is an example:

def multiply (inta, intb):
    print(inta * intb)

The above script defines a function named multiply() with two non-default arguments inta and intb. The funcion simply prints the product of the values passed to the inta and intb arguments.

When you call the multiply() function, you’ll always need to pass values for both the inta and intb arguments, as shown below:

multiply(5,8)

Here is the output:

40

What happens when we try to call the multiply() function without passing it any arguments?:

multiply()

Output:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-7271e03b3b52> in <module>
----> 1 multiply()

TypeError: multiply() missing 2 required positional arguments: 'inta' and 'intb'

You can see when we call the multiply() function without any arguments, an exception occurs that clearly tells us we are missing 2 required positional arguments: inta and intb which means that we need to pass the values for these two arguments.

Can't get enough Python?

Enter your email address for more free Python tutorials and tips.

Python is powerful! Show me more free Python tips

Function Overloading with Python Default Arguments

With default arguments in Python, we can call the same function with or without passing argument values in our function calls. Calling a function with the same name but with a different number of parameters is called function overloading. Function overloading is an extremely important concept of object oriented programming.

Let’s see how we can use default arguments to implement function overloading. The following script defines a method take_cube() with one default argument inta. To create a default argument, you simply have to assign something to the argument. In the following script we assign 5 as the default value for the default argument inta.

def take_cube(inta = 5):
    print(inta * inta * inta)

Now if you call the take_cube() function by passing a value, the passed value will overwrite the default value for the inta argument and the function will print the cube of the passed value. For instance, in the following script, we pass 4 as the argument value in the call to the take_cube() function. The value 4 will overwrite the default value 5, and the take_cube() function will print the cube of 4 in the output:

take_cube(4)

Output:

64

If you call the take_cube() function without passing any value for the inta argument, the cube of the default value of 5 will be returned, as shown below:

take_cube()

Output:

125

You can see that we called the same function, take_cube(), with a different number of parameters. This is called function overloading.

A function can have multiple default parameters. For example in the following script, the multiply() function has two default parameters.

def multiply (inta = 2, intb=4):
    print(inta * intb)

Let’s first call the multiply() function by passing explicit argument values:

multiply(5,8)

In the output of the above script, you’ll notice the product of integers 5 and 8. If you call the multiply() function without any arguments in the function call, the product of the default arguments, 2 and 4, will be printed.

Finally, a function can have default and non-default arguments. It’s important to mention that a non-default argument cannot be mentioned after a default argument in the arguments list. For instance, the following script will throw an error.

def multiply (inta = 4, intb):
    print(inta * intb)

In the script above, the non-default argument, intb is mentioned after the default argument inta. This is not allowed so an exception is thrown:

File "<ipython-input-23-6480240ec346>", line 1
  def multiply (inta = 4, intb):
               ^
SyntaxError: non-default argument follows default argument

Let’s see another example of function overloading with three arguments. We’ll redefine our multiply() function as:

def multiply(a, b, c = 10):
    print( a * b * c)

In the above script, the multiply() function accepts three argument values: a, b, and c. The function returns the product of values passed to the three arguments. The first two arguments are non-default arguments while the third argument is a default argument. While calling the multiply() function, you will need to pass the values for the first two argument, while passing a value for the third argument, c, is optional. If you do not pass the value for the third argument, the function will assume a value of 10.

Let’s now call the multiply() function with two and three arguments in the function call.

multiply(2, 4)

The above script will print 80 since the values for the arguments a and b are set to 2 and 4, respectively in the function call. For the default argument c the default value of 10 will be used, hence the product will be 2x4x10=80.

If you call the multiply() function by explicity passing values for all three arguments, you’ll see the product of those three values. `

multiply(2, 4, 5)

The output of the above script will be 40 (2x4x5). In this case, the integer 5 is passed as the value for the default argument c which overwrites the default value 10 and hence the product 2x4x5=40 is presented in the output.

Now we’ll try redefining our multiply function with three arguments, but this time the second argument will be assigned a default value.

def multiply(a, b = 10, c):
    print(a * b * c)

If you try to execute the above code, you’ll again see an error:

File "<ipython-input-6-2ad3394c94dc>", line 1
    def multiply(a, b = 10, c):
                ^
SyntaxError: non-default argument follows default argument

The exceptions occurs because non-default arguments cannot follow default arguments in Python. In the updated multiply() function, the second argument b is a default argument. After the default argument we defined a non-default argument c. Since a non-default argument was defined after a default argument, the exception occurs.

The bottom line is you must remember if you have both default and non-default arguments in your function, all the non-default arguments must be defined before the default arguments.

Can't get enough Python?

Enter your email address for more free Python tutorials and tips.

Python is powerful! Show me more free Python tips