Introduction to Python Default Arguments
Default arguments refer to those arguments which are being utilized by a function when no other value is passed to that function, Provided that function expects an argument. Python programming language allows the functions to have a default argument in place. If an argument isn’t passed during that function call, then instead of a runtime failure, that function utilizes that default argument.
Python has quite a different syntax for representing the default argument values. Default arguments assigned to a function indicate that this will be used if in case no argument is passed. The value is assigned to the default argument using the assignment operator.
Syntax:
The syntax is defined down below:
def default_Aargument(default_arg = 'This is defaults argument'):
print(default_arg)
Here we are defining or rather initiating the argument named ‘default_arg’ within the function name with a value as ‘This is defaults argument’.
Examples to Implement Default Arguments
Let us see some examples
Example #1
Python program to understand the syntax of default arguments
Now, when a call is made to this function without argument value, then the default value will be printed in this case
Code:
def default_Aargument(default_arg = 'This is defaults argument'):
print(default_arg)
default_Aargument()
Output:
What Will happen if, We Pass an Argument when We Call This Function?
Code:
## Python program to understand the syntax of default arguments with an argument passed to the function
def default_Aargument(default_arg = 'This is defaults argument'):
print(default_arg)
default_Aargument('This is argument passed to the function')
What will be the output of this program?
- Case 1: This is the defaults argument
- Case 2: This is the argument passed to the function
The argument passed will override the default argument.
Output:
How does this work Behind the Scenes?
If we talk about the control flow of the above program the,
- As soon as we call the function using the below piece of code, we are passing an argument along with that as well, and the control is transferred to the function defined above
- default_Aargument(‘This is argument passed to the function’)
- As the control is transferred to the function, it makes a quick check that during the call to this function if an argument is passed or not.
- If an argument is passed during the call, it will be used and assigned to the variable where we are catching it.
- Else, if we aren’t passing any argument while calling the function, then this default argument that we are assigning the function will be used.
- That’s the reason behind the fact that the argument passed will override the default argument.
Let’s take an example to understand the same.
Example #2
What will happen if we call a function with an argument, Provided the function expects an argument and the default argument isn’t specified?
Code:
## Python program to understand the importance of default arguments
def default_Aargument(default_arg):
print(default_arg)
default_Aargument()
Output:
Why have we Encountered this Error?
- As soon as a call is made to the function using the below statement, the control is transferred to the function defined above
- default_Aargument()
- But over here, we aren’t passing any argument over here
- Moreover, we have not defined any default argument as well within the function that we are calling
- So, as soon as we call the function without the argument, and the interpreter doesn’t even find any default value specified to the argument, then it throws an error as it has no option out there.
- If we had defined a default value for this argument, then it would not have failed.
Code:
def default_Aargument(default_arg = 'Def arg'):
print(default_arg)
default_Aargument()
Output:
Example #3
Default arguments in case of multiple arguments
What if we have two or more arguments in the function? How will python come to know how to assign the value if we are passing only one while calling the function?
CASE 1:
def default_Aargument(Arg1,
default_arg = 'This is defaults argument'):
print(default_arg)
print(Arg1)
default_Aargument('This is argument passed to the function')
Output:
CASE 2:
def default_Aargument( default_arg = 'This is defaults argument',Arg1,):
print(default_arg)
print(Arg1)
default_Aargument('This is argument passed to the function')
Output:
The arguments passed are caught in the same order. So that’s why we are getting an error in case 2, as Arg1 did not receive any value during the function call.
Recommended Articles
This is a guide to Python Default Arguments. Here we discuss Syntax to Python Default Arguments, along with respective examples for better understanding. You can also go through our other related articles to learn more –