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.

Example

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(age=50,name="freshersnow")     
printinfo( name="freshersnow" )

Output:
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.

Example

def printme( str ): "This prints a passed string into this function"               
print str              
return;   
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.

 Example

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

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.

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

Example

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

Output
10
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 

Example

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

Output
value of total: 30
value of total: 40

Python Return Statement

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

Example

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

Output
Inside the function: 30
Outside the function: 30