Function Arguments


The following are the types of arguments that we can use to call a function:

  1. Default arguments
  2. Keyword arguments
  3. Required arguments
  4. 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.


  1. # Python code to demonstrate the use of default arguments  
  2. # defining a function  
  3. def function( num1, num2 = 40 ):  
  4.     print("num1 is: ", num1)  
  5.     print("num2 is: ", num2)  
  6.    
  7.    # Calling the function and passing only one argument  
  8. print"Passing one argument" )  
  9. function(10)  
  10.   
  11. # Now giving two arguments to the function  
  12. print"Passing two arguments" )  
  13. function(10,30)  


Output:
Passing one argument
num1 is:  10
num2 is:  40
Passing two arguments
num1 is:  10
num2 is:  30


============================================================

Keyword Arguments


  1. # Python code to demonstrate the use of keyword arguments  
  2.   
  3. # Defining a function  
  4. def function( num1, num2 ):  
  5.     print("num1 is: ", num1)  
  6.     print("num2 is: ", num2)  
  7.   
  8. # Calling function and passing arguments without using keyword  
  9. print"Without using keyword" )  
  10. function( 5030)     
  11.       
  12. # Calling function and passing arguments using keyword  
  13. print"With using keyword" )  
  14. 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:

  1. *args -These are Non-Keyword Arguments
  2. **kwargs - These are Keyword Arguments.


  1. # Python code to demonstrate the use of variable-length arguments  
  2.    
  3. # Defining a function  
  4. def function( *args_list ):  
  5.     ans = []  
  6.     for l in args_list:  
  7.         ans.append( l.upper() )  
  8.     return ans  
  9. # Passing args arguments  
  10. object = function('Python''Functions''tutorial')  
  11. print( object )  
  12.   
  13. # defining a function  
  14. def function( **kargs_list ):  
  15.     ans = []  
  16.     for key, value in kargs_list.items():  
  17.         ans.append([key, value])  
  18.     return ans  
  19. # Paasing kwargs arguments  
  20. object = function(First = "Python", Second = "Functions", Third = "Tutorial")  
  21. print(object)  


Output:

['PYTHON', 'FUNCTIONS', 'TUTORIAL']
[['First', 'Python'], ['Second', 'Functions'], ['Third', 'Tutorial']]