i

Python Programming

Arguments in Function

Calling function by using following formal arguments:

•    Required-arguments

•    Keyword-arguments

•    Default-arguments

•    Variable length arguments

Required-arguments

Required-arguments are the arguments passed to function in corrected positional order. Number of arguments in the function should match the exact function definitions.

Calling function printm(), you definitely should pass one argument and it gives syntax error shown as below:

Live Demo

Output of above code is as follows:

Error traceback(most of recent call last):

file "test.py" line 10 in

printm();

TypeError: printm() takes exactly 1 argument (0 given)

Keyword-arguments

Keyword-arguments are related to function calls. Using keyword-arguments in function call, caller identifies arguments by parameter name.

Allows you to skip argument or place them out of order to the python interpreter is able to use the keywords to match values in parameter. You can make keyword calls also to the printm() function in following ways:

Live Demo

Output of the above code:

My string

Below example shows order of parameters does not matter.

Live Demo

Output:

EName:  miki

eage  50

Default-arguments

Default-argument is an argument that assumes a default value if value is not provided in the function call for that argument. Please check below example where you get idea of default arguments:

Live Demo

Output:

eName:  miki

eAge  50

eName:  miki

eAge  35

Variable-length-arguments

You need to process the function for more arguments than you specified defining the function. This type of arguments are called variable-length-arguments and are not named in the function definition.

Below is the syntax of non-keyword-variable-arguments:

Asterisk (*) is placed before the variable name which holds the values of all non-keyword variable arguments. This type of tuple remains empty if no additional arguments are specified during the function call. Below is the example:

Live Demo

Output:                   

10

70

60

50

The Anonymous-Functions

This type of functions are called anonymous because they are not declared in the standard manner by def keyword. For that we can use the lambda keyword to create small anonymous-functions.

•    The Lambda-function may take any number of arguments but return just one value in the form of expression. There cannot be any commands or multiple expression.

•  An anonymous-function cannot be a direct-call to print because lambda-function requires an expression

•    The Lambda-functions generally have their own local namespace and cannot access variables other than those in parameter list and in the global namespace.

•    The Lambda are a one-line version of a function,  they are not equivalent to inline statements in C or C++.

Syntax

Below is the syntax of the Lambda-functions contain only a single string:

lambda[argu1 [,argu2,.....argu]]:expression

Live Demo

Output:

total :  30

total :  40

The return-Statement

The return-statement  return [expression] exits function, optionally passing back an expression to the caller. Return-statement with no arguments is the same as return-None.

Live Demo

Output:

In the function :  30

Out the function :  30