The following are the types of arguments that we can use to call a function:
- Default arguments
- Keyword arguments
- Required arguments
- Variable-length arguments
=======================================================
Default Arguments
A default argument is a kind of parameter that takes as input a default value if no value is supplied for the argument when the function is called. Default arguments are demonstrated in the following instance.
- # Python code to demonstrate the use of default arguments
- # defining a function
- def function( num1, num2 = 40 ):
- print("num1 is: ", num1)
- print("num2 is: ", num2)
- # Calling the function and passing only one argument
- print( "Passing one argument" )
- function(10)
- # Now giving two arguments to the function
- print( "Passing two arguments" )
- function(10,30)
Output:
Passing one argument
num1 is: 10
num2 is: 40
Passing two arguments
num1 is: 10
num2 is: 30
============================================================
Keyword Arguments
- # Python code to demonstrate the use of keyword arguments
- # Defining a function
- def function( num1, num2 ):
- print("num1 is: ", num1)
- print("num2 is: ", num2)
- # Calling function and passing arguments without using keyword
- print( "Without using keyword" )
- function( 50, 30)
- # Calling function and passing arguments using keyword
- print( "With using keyword" )
- function( num2 = 50, num1 = 30)
Output
Without using keyword
num1 is: 50
num2 is: 30
With using keyword
num1 is: 30
num2 is: 50
============================================================
Required Arguments
# Python code to demonstrate the use of default arguments
# Defining a function
def function( num1, num2 ):
print("num1 is: ", num1)
print("num2 is: ", num2)
# Calling function and passing two arguments out of order, we need num1 to be 20 and num2 to be 30
print( "Passing out of order arguments" )
function( 30, 20 )
# Calling function and passing only one argument
print( "Passing only one argument" )
try:
function( 30 )
except:
print( "Function needs two positional arguments" )
Output:
Passing out of order arguments
num1 is: 30
num2 is: 20
Passing only one argument
Function needs two positional arguments
============================================================
Variable-Length Arguments
We can use special characters in Python functions to pass as many arguments as we want in a function. There are two types of characters that we can use for this purpose:
- *args -These are Non-Keyword Arguments
- **kwargs - These are Keyword Arguments.
- # Python code to demonstrate the use of variable-length arguments
- # Defining a function
- def function( *args_list ):
- ans = []
- for l in args_list:
- ans.append( l.upper() )
- return ans
- # Passing args arguments
- object = function('Python', 'Functions', 'tutorial')
- print( object )
- # defining a function
- def function( **kargs_list ):
- ans = []
- for key, value in kargs_list.items():
- ans.append([key, value])
- return ans
- # Paasing kwargs arguments
- object = function(First = "Python", Second = "Functions", Third = "Tutorial")
- print(object)
Output:
['PYTHON', 'FUNCTIONS', 'TUTORIAL']
[['First', 'Python'], ['Second', 'Functions'], ['Third', 'Tutorial']]