< Prev
Next >

# Python - Functions with Default Arguments

In our last article, we have explained the concept of defining and calling functions with arguments. Moving ahead, in this article, we are going to explain a very important concept i.e. functions with default arguments.

In Python, a function can be defined with some arguments which are initialized with a value within the parenthesis( ) of the same function and these arguments are also termed as default arguments of the function. Let us understand some features of functions with default arguments with some examples.

## Important points about functions with default arguments

• A function defined with n number of default arguments can be passed zero to n number of arguments, but never more than n.
• While calling a function defined with default arguments, we may or may not pass any arguments at all.
• We can define a function with a mix of non-default arguments and default arguments.
• If a function is defined with a mix of non-default arguments and default arguments then the default arguments should be placed after non-default arguments in the parenthesis( ).

## Note :

A point to remember - As the name suggests, we can only define default arguments within a function's parenthesis i.e. when we are defining a function.

• ## Example - function with default arguments

• A function defined with n number of default arguments can be passed zero to n number of arguments, but never more than n. Consequently, in the upcoming example, we are going to define a function with two default arguments and then we are going to call this function with :
• no arguments.
• one argument.
• two arguments.

``````# Python - Function with default arguments

# Defining a function with two default arguments
def add(a =10, b = 20):
print("result : ", a+b)

# Calling the add() function without any arguments

# Calling the add() function with only one argument

# Calling the add() function with two arguments

## Output

``````result :  30
result :  50.5
result :  70``````

In the program just above, we have called a function add(defined with two default arguments), with either zero, one or two arguments.

• ## A function with n number of default arguments cannot be called with more than n arguments.

• Calling a function(defined with default arguments) with the number of arguments more than the number of default arguments defined in it gives an error. Let us show you this point with an example.

``````# Python - Function with default arguments

# Defining a function with two default arguments
def display(a = 10, b = 24.99):
print('a : ', a)
print('b : ', b)

print("Calling add() function without any arguments :")
display()

print("Calling add() function with one arguments :")
display(20)

print("Calling add() function with two arguments :")
display(20,30)

print("Calling add() function with two arguments :")
display(20,30,40)``````

## Output

``````print("Calling add() function without any arguments :")
a :  10
b :  24.99
print("Calling add() function with one arguments :")
a :  20
b :  24.99
print("Calling add() function with two arguments :")
a :  20
b :  30
Traceback (most recent call last):
File "D:/Python Programs/default2.py", line 23, in <module>
display(20,30,40)
TypeError: display() takes from 0 to 2 positional arguments but 3 were given``````

Calling the function display(defined with two default arguments) with zero, one or two arguments have gone smooth but calling it with three arguments has resulted in an error, proving that a function defined with n number of default arguments should never be called with more than n number of arguments.

• ## We can define a function with a mix of non-default arguments and default arguments.

• Yes, we could even define a function by mixing non-default arguments with default arguments. Let us prove this with an example.

``````# Python - Function with mixing non-default and default arguments

# Defining a function with a non-default(x) and a default argument(s)
def display(x, s = 'Hi'):
print('x : ', x)
print('s : ', s)

print('Calling the show() function with only one argument')
show(20)

print('Calling the show() function with two arguments')
show(55.9, 'Hello')``````

## Output

``````Calling the show() function with only one argument
x :  20
s :  Hi
Calling the show() function with two arguments
x :  55.9
s :  Hello``````

We have called a function named show(defined with one non-default and one default argument), and have passed it at least one argument, because calling such function without any argument would lead to an error, as there is one non-default(uninitialized) argument in it.

• ## Default arguments should be placed after non-default arguments in a function definition.

When defining a function by mixing non-default arguments with default arguments, the default arguments should be placed last. Let us prove this with an example.

``````# Python - Function with mixing non-default and default arguments

# Defining a function with a non-default(x) and a default argument(s)
def display(s = 'Hi',x):
print('x : ', x)
print('s : ', s)

print('Calling the show() function with only one argument')
show('Hello', 20)

print('Calling the show() function with two arguments')
show('Hello', 50)
``````

This program reports a SyntaxError which says non-argument follows default argument. Consequently, when defining a function by mixing non-default arguments with default arguments, default arguments should be placed last or an error is reported.

< Prev
Next >
< Python functions with arguments
Python recursive function >