Function Arguments in Python

Function arguments in python: A function can be called by using a different type of arguments. They are as follows:

  • Default Arguments 
  • Keyword Arguments 
  • Required Arguments 
  • Variable-length Arguments 

Function Arguments in Python

Below we have explained various function arguments in Python with suitable examples.

Python Default Arguments

It is an argument that assumes as a default value if a value is not provided in the function call of that argument.


def printinfo( name, age = 25 ):        
"This prints a passed info into this function"          
print "Name: ",name 
print "Age ",age          
return; #Now you can call printinfo function      
printinfo( name="freshersnow" )

Name: frehsersnow Age  25
Name: freshersnow Age 25

Python Keyword Arguments

These are arguments are related to the function calls. When we are using keyword arguments in a function call, the caller identifies the arguments by parameter name.

You can skip the arguments or place them out of the order because the python interpreter is able to use the keywords provided to match the values with parameters.


def printme( str ): "This prints a passed string into this function"               
print str              
printme(str="freshers now")

Output: freshers now

Python Required Arguments

These are the arguments passed to a function in correct positional order. And the no .of functional arguments should match exactly with the function definition.


def printme( str ):"This prints a passed string into this function"          
print str          

Output: TypeError: printme() takes exactly 1 argument (0 given)

 Python Variable – Length Arguments

If you need to process a function argument than you have specified while defining the function. These arguments are called variable-length arguments.

def function-name([formal_args,] *var_args_tuple ):  “function_docstring”                
return [expression]  


def printinfo( arg1, *vartuple ):                   
print "Output is:"print arg1 
for var in vartuple:
print var                     
printinfo( 10 )             
printinfo( 70, 60, 50 )


Python Anonymous Functions or Lambda Function

These listed functions are called anonymous functions because they don’t declare in the standard manner by using the def keyword. You can use the Lambda keyword to create small anonymous functions.

  • Lambda forms can take any number of arguments, but return one value just in the form of expression.
  • They cannot contain commands or multiple expressions.
  • An anonymous function cannot be a direct call to print because Lambda requires an expression.
  •  Lambda functions have their own local namespace and cannot access variables other than those in their parameter list and those in the global namespace.

Syntax: lambda [arg1 [, arg2,…..argn]]:expression 


sum=lambda arg1, arg2: arg1 + arg2;        
print "Value of total : ", sum( 10, 20 )        
print "Value of total : ", sum( 20, 20 )

value of total: 30
value of total: 40

Python Return Statement

It exits a function, optionally passing back an expression to the caller.


def sum (arg1, arg2):              
total = arg1 + arg2#Add both the parameters and return them."            
print "Inside the function: ",total               
return total;             
total = sum( 10, 20 );            
print "Outside the function: ",total

Inside the function: 30
Outside the function: 30