functions

=Functions= A function is a block of organized, reusable code that is used to perform a single, related action. Functions provides better modularity for your application and a high degree of code reusing. As you already know, Python gives you many built-in functions like print etc. but you can also create your own functions. These functions are called //user-defined functions.// = Defining a Function = You can define functions to provide the required functionality. Here are simple rules to define a function in Python: > > > > >
 *  Function blocks begin with the keyword **def** followed by the function name and parentheses.
 *  Any input parameters or arguments should be placed within these parentheses. You can also define parameters inside these parentheses.
 *  The first statement of a function can be an optional statement - the documentation string of the function or //docstring//.
 *  The code block within every function starts with a colon and is indented.
 *  The statement return [expression] exits a function, optionally passing back an expression to the caller. A return statement with no arguments is the same as return None.

Syntax:
code def functionname( parameters ): "function_docstring" function_suite return [expression] code || By default, parameters have a positional behavior, and you need to inform them in the same order that they were defined.

Example:
Here is the simplest form of a Python function. This function takes a string as input parameter and prints it on standard screen. code def printme( str ): "This prints a passed string into this function" print str return code || = Calling a Function = Defining a function only gives it a name, specifies the parameters that are to be included in the function, and structures the blocks of code. Once the basic structure of a function is finalized, you can execute it by calling it from another function or directly from the Python prompt. Following is the example to call printme function: code #!/usr/bin/python

def printme( str ): "This prints a passed string into this function" print str; return;
 * 1) Function definition is here

printme("I'm first call to user defined function!"); printme("Again second call to the same function"); code || <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">This would produce following result: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">I'm first call to user defined function! Again second call to the same function code || = Pass by reference vs value = <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">All parameters (arguments) in the Python language are passed by reference. It means if you change what a parameter refers to within a function, the change also reflects back in the calling function. For example: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">#!/usr/bin/python
 * 1) Now you can call printme function

def changeme( mylist ): "This changes a passed list into this function" mylist.append([1,2,3,4]); print "Values inside the function: ", mylist return
 * 1) Function definition is here

mylist = [10,20,30]; changeme( mylist ); print "Values outside the function: ", mylist code || <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">Here we are maintaining reference of the passed object and appending values in the same object. So this would produce following result: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">Values inside the function: [10, 20, 30, [1, 2, 3, 4]] Values outside the function: [10, 20, 30, [1, 2, 3, 4]] code || <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">There is one more example where argument is being passed by reference but inside the function, but the reference is being over-written. code <span style="font-family: 'Courier New',monospace; font-size: 12px;">#!/usr/bin/python
 * 1) Now you can call changeme function

def changeme( mylist ): "This changes a passed list into this function" mylist = [1,2,3,4]; # This would assig new reference in mylist print "Values inside the function: ", mylist return
 * 1) Function definition is here

mylist = [10,20,30]; changeme( mylist ); print "Values outside the function: ", mylist code || <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">The parameter mylist is local to the function changeme. Changing mylist within the function does not affect mylist. The function accomplishes nothing and finally this would produce following result: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">Values inside the function: [1, 2, 3, 4] Values outside the function: [10, 20, 30] code || = Function Arguments: = <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">You can call a function by using the following types of formal arguments::
 * 1) Now you can call changeme function
 * <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: left;">Required arguments
 * <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: left;">Keyword arguments
 * <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: left;">Default arguments
 * <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: left;">Variable-length arguments

Required arguments:
<span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">Required arguments are the arguments passed to a function in correct positional order. Here the number of arguments in the function call should match exactly with the function definition. <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">To call the function //printme// you definitely need to pass one argument otherwise it would give a syntax error as follows: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">#!/usr/bin/python

def printme( str ): "This prints a passed string into this function" print str; return;
 * 1) Function definition is here

printme; code || <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">This would produce following result: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">Traceback (most recent call last): File "test.py", line 11, in    printme; TypeError: printme takes exactly 1 argument (0 given) code ||
 * 1) Now you can call printme function

Keyword arguments:
<span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">Keyword arguments are related to the function calls. When you use keyword arguments in a function call, the caller identifies the arguments by the parameter name. <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">This allows you to skip arguments or place them out of order because the Python interpreter is able to use the keywords provided to match the values with parameters. <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">You can also make keyword calls to the //printme// function in the following ways: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">#!/usr/bin/python

def printme( str ): "This prints a passed string into this function" print str; return;
 * 1) Function definition is here

printme( str = "My string"); code || <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">This would produce following result: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">My string code || <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">Following example gives more clear picture. Note, here order of the parameter does not matter: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">#!/usr/bin/python
 * 1) Now you can call printme function

def printinfo( name, age ): "This prints a passed info into this function" print "Name: ", name; print "Age ", age; return;
 * 1) Function definition is here

printinfo( age=50, name="miki" ); code || <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">This would produce following result: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">Name: miki Age 50 code ||
 * 1) Now you can call printinfo function

Default arguments:
<span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">A default argument is an argument that assumes a default value if a value is not provided in the function call for that argument. <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">Following example gives idea on default arguments, it would print default age if it is not passed: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">#!/usr/bin/python

def printinfo( name, age = 35 ): "This prints a passed info into this function" print "Name: ", name; print "Age ", age; return;
 * 1) Function definition is here

printinfo( age=50, name="miki" ); printinfo( name="miki" ); code || <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">This would produce following result: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">Name: miki Age 50 Name: miki Age 35 code ||
 * 1) Now you can call printinfo function

Variable-length arguments:
<span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">You may need to process a function for more arguments than you specified while defining the function. These arguments are called //variable-length// arguments and are not named in the function definition, unlike required and default arguments. <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">The general syntax for a function with non-keyword variable arguments is this: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">def functionname([formal_args,] *var_args_tuple ): "function_docstring" function_suite return [expression] code || <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">An asterisk (*) is placed before the variable name that will hold the values of all nonkeyword variable arguments. This tuple remains empty if no additional arguments are specified during the function call. For example: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">#!/usr/bin/python

def printinfo( arg1, *vartuple ): "This prints a variable passed arguments" print "Output is: " print arg1 for var in vartuple: print var return;
 * 1) Function definition is here

printinfo( 10 ); printinfo( 70, 60, 50 ); code || <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">This would produce following result: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">Output is: 10 Output is: 70 60 50 code || = The //Anonymous// Functions: = <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">You can use the //lambda// keyword to create small anonymous functions. These functions are called anonymous because they are not declared in the standard manner by using the //def//keyword. > > > >
 * 1) Now you can call printinfo function
 * <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: left;"> Lambda forms can take any number of arguments but return just one value in the form of an expression. They cannot contain commands or multiple expressions.
 * <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: left;"> An anonymous function cannot be a direct call to print because lambda requires an expression.
 * <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: left;"> Lambda functions have their own local namespace and cannot access variables other than those in their parameter list and those in the global namespace.
 * <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: left;"> Although it appears that lambda's are a one-line version of a function, they are not equivalent to //inline// statements in C or C++, whose purpose is by passing function stack allocation during invocation for performance reasons.

Syntax:
<span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">The syntax of //lambda// functions contains only a single statement, which is as follows: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">lambda [arg1 [,arg2,.....argn]]:expression code ||

Example:
<span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">Following is the example to show how //lembda// form of function works: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">#!/usr/bin/python

sum = lambda arg1, arg2: arg1 + arg2;
 * 1) Function definition is here

print "Value of total : ", sum( 10, 20 ) print "Value of total : ", sum( 20, 20 ) code || <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">This would produce following result: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">Value of total : 30 Value of total : 40 code || = The //return// Statement: = <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">The statement return [expression] exits a function, optionally passing back an expression to the caller. A return statement with no arguments is the same as return None <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">All the above examples are not returning any value, but if you like you can return a value from a function as follows: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">#!/usr/bin/python
 * 1) Now you can call sum as a function

def sum( arg1, arg2 ): # Add both the parameters and return them."  total = arg1 + arg2   print "Inside the function : ", total   return total;
 * 1) Function definition is here

total = sum( 10, 20 ); print "Outside the function : ", total code || <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">This would produce following result: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">Inside the function : 30 Outside the function : 30 code || = Scope of Variables: = <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">All variables in a program may not be accessible at all locations in that program. This depends on where you have declared a variable. <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">The scope of a variable determines the portion of the program where you can access a particular identifier. There are two basic scopes of variables in Python:
 * 1) Now you can call sum function
 * <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: left;">Global variables
 * <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: left;">Local variables

Global vs. Local variables:
<span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">Variables that are defined inside a function body have a local scope, and those defined outside have a global scope. <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">This means that local variables can be accessed only inside the function in which they are declared whereas global variables can be accessed throughout the program body by all functions. When you call a function, the variables declared inside it are brought into scope.

Example:
code <span style="font-family: 'Courier New',monospace; font-size: 12px;">#!/usr/bin/python

total = 0; # This is global variable. def sum( arg1, arg2 ): # Add both the parameters and return them."  total = arg1 + arg2; # Here total is local variable.   print "Inside the function local total : ", total   return total;
 * 1) Function definition is here

sum( 10, 20 ); print "Outside the function global total : ", total code || <span style="background-color: #ffffff; font-family: verdana,helvetica,arial,sans-serif; font-size: 11px; text-align: justify;">This would produce following result: code <span style="font-family: 'Courier New',monospace; font-size: 12px;">Inside the function local total : 30 Outside the function global total : 0 code ||
 * 1) Now you can call sum function