Functions

A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing. Other website and text books might refer to a function as a procedure or sub-program and those terms are reasonably interchangeable.

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:

Syntax


def functionname( parameters ):
   "function_docstring"
   function_suite
   return [expression]


By default, parameters have a positional behavior and you need to inform them in the same order that they were defined.


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


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 −


#!/usr/bin/python

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

# Now you can call printme function
printme("I'm first call to user defined function!")
printme("Again second call to the same function")


When the above code is executed, it produces the following result:


I'm first call to user defined function!
Again second call to the same function


Anonymous Functions

These functions are called anonymous because they are not declared in the standard manner by using the def keyword. You can use the lambda keyword to create small anonymous functions.

Syntax

The syntax of lambda functions contains only a single statement:

Example

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


Following is an example to show how thelambda form of function works:


#!/usr/bin/python

# Function definition is here
sum = lambda arg1, arg2: arg1 + arg2;

# Now you can call sum as a function
print "Value of total : ", sum( 10, 20 )
print "Value of total : ", sum( 20, 20 )


When the above code is executed, it produces the following result:


Value of total :  30
Value of total :  40


The return statement

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.

All the above examples are not returning any value. You can return a value from a function as follows:


#!/usr/bin/python

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

# Now you can call sum function
total = sum( 10, 20 );
print "Outside the function : ", total


When the above code is executed, it produces the following result:


Inside the function :  30
Outside the function :  30