Functions in Python

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.

As you already know, Python gives you many built-in functions such as print() and 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

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.

Example

The following function takes a string as input parameter and prints it on standard screen.

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

Passing by Reference Versus Passing by Value

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:

#!/usr/bin/python

  • Function definition is here def changeme( mylist ):

"This changes a passed list into this function" mylist.append([1,2,3,4]);

print "Values inside the function: ", mylist return

  • Now you can call changeme function

mylist = [10,20,30]; changeme( mylist );

print "Values outside the function: ", mylist

Here, we are maintaining reference of the passed object and appending values in the same object. So, this would produce the following result:

Values inside the function:     [10, 20, 30, [1, 2, 3, 4]]

Values outside the function:     [10, 20, 30, [1, 2, 3, 4]]

There is one more example where argument is being passed by reference and the reference is being overwritten inside the called function.

#!/usr/bin/python

# Function definition is here 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

# Now you can call changeme function mylist = [10,20,30];

changeme( mylist );

print "Values outside the function: ", mylist

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 the following result:

Values inside the function:     [1, 2, 3, 4]

Values outside the function:     [10, 20, 30]

Function Arguments

You can call a function by using the following types of formal arguments:

  • Required arguments
  • Keyword arguments
  • Default arguments
  • Variable-length arguments

Required Arguments

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.

To call the function printme(), you definitely need to pass one argument, otherwise it gives a syntax error as follows:

#!/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();

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

Traceback (most recent call last):

File "test.py", line 11, in <module>

printme();

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

Keyword Arguments

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.

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. You can also make keyword calls to the printme() function in the following ways:

#!/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( str = "My string");

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

My string

The following example gives more clear picture. Note that the order of parameters does not matter.

#!/usr/bin/python

  • Function definition is here def printinfo( name, age ):

"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="miki" );

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

Name:     miki

Age   50

Default Arguments

A default argument is an argument that assumes a default value if a value is not provided in the function call for that argument. The following example gives an idea on default arguments, it prints default age if it is not passed:

#!/usr/bin/python

  • Function definition is here def printinfo( name, age = 35 ):

"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="miki" ); printinfo( name="miki" );

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

Name:     miki

Age     50

Name:     miki

Age     35

Variable Length Arguments

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.

Syntax for a function with non-keyword variable arguments is this:

def functionname([formal_args,] *var_args_tuple ):

"function_docstring"

function_suite

return [expression]

An asterisk (*) is placed before the variable name that holds the values of all nonkeyword variable arguments. This tuple remains empty if no additional arguments are specified during the function call. Following is a simple example:

#!/usr/bin/python

# Function definition is here

def printinfo( arg1, *vartuple ):

"This prints a variable passed arguments" print "Output is: "

print arg1

for var in vartuple: print var

return;

# Now you can call printinfo function printinfo( 10 );

printinfo( 70, 60, 50 );

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

Output is:

10

Output is:

70

60

50

The 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.

  • 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.
  • 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.
  • 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

The syntax of lambda functions contains only a single statement, which is as follows:

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

Following is the example to show how lambda 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

Scope of Variables

All variables in a program may not be accessible at all locations in that program. This depends on where you have declared a variable.

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:

  • Global variables
  • Local variables

Global vs. Local variables:

Variables that are defined inside a function body have a local scope, and those defined outside have a global scope.

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. Following is a simple example:

#!/usr/bin/python

total = 0; # This is global variable.

# Function definition is here 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;

# Now you can call sum function sum( 10, 20 );

print "Outside the function global total : ", total

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

Inside the function local total :     30

Outside the function global total :     0

 

Python lambda (Anonymous Functions) | filter, map, reduce

In Python, anonymous function means that a function is without a name. As we already know that def keyword is used to define the normal functions and the lambda keyword is used to create anonymous functions. It has the following syntax:

lambda arguments: expression
  • This function can have any number of arguments but only one expression, which is evaluated and returned.
  • One is free to use lambda functions wherever function objects are required.
  • You need to keep in your knowledge that lambda functions are syntactically restricted to a single expression.
  • It has various uses in particular fields of programming besides other types of expressions in functions.

Let’s look at this example and try to understand the difference between a normal def defined function and lambda function. This is a program that returns the cube of a given value:

filter_noneedit

play_arrow

brightness_4

# Python code to illustrate cube of a number 
# showing difference between def() and lambda().
def cube(y):
    return y*y*y;
 
g = lambda x: x*x*x
print(g(7))
 
print(cube(5))

Output:

343
125
  • Without using Lambda : Here, both of them returns the cube of a given number. But, while using def, we needed to define a function with a name cube and needed to pass a value to it. After execution, we also needed to return the result from where the function was called using the return keyword.
  • Using Lambda : Lambda definition does not include a “return” statement, it always contains an expression which is returned. We can also put a lambda definition anywhere a function is expected, and we don’t have to assign it to a variable at all. This is the simplicity of lambda functions.

Lambda functions can be used along with built-in functions like filter(), map() and reduce().

Use of lambda() with filter()


The filter() function in Python takes in a function and a list as arguments. This offers an elegant way to filter out all the elements of a sequence “sequence”, for which the function returns True. Here is a small program that returns the odd numbers from an input list:

filter_noneedit

play_arrow

brightness_4

# Python code to illustrate
# filter() with lambda()
li = [5, 7, 22, 97, 54, 62, 77, 23, 73, 61]
final_list = list(filter(lambda x: (x%2 != 0) , li))
print(final_list)

Output:

[5, 7, 97, 77, 23, 73, 61]

Use of lambda() with map()

The map() function in Python takes in a function and a list as argument. The function is called with a lambda function and a list and a new list is returned which contains all the lambda modified items returned by that function for each item. Example:

filter_noneedit

play_arrow

brightness_4

# Python code to illustrate 
# map() with lambda() 
# to get double of a list.
li = [5, 7, 22, 97, 54, 62, 77, 23, 73, 61]
final_list = list(map(lambda x: x*2 , li))
print(final_list)

Output:

[10, 14, 44, 194, 108, 124, 154, 46, 146, 122]

Use of lambda() with reduce()

The reduce() function in Python takes in a function and a list as argument. The function is called with a lambda function and a list and a new reduced result is returned. This performs a repetitive operation over the pairs of the list. This is a part of functools module. Example:

filter_noneedit

play_arrow

brightness_4

# Python code to illustrate 
# reduce() with lambda()
# to get sum of a list
from functools import reduce
li = [5, 8, 10, 20, 50, 100]
sum = reduce((lambda x, y: x + y), li)
print (sum)

Output:

193

Here the results of previous two elements are added to the next element and this goes on till the end of the list like (((((5+8)+10)+20)+50)+100).

######################

def sqr(x):
return x*x

print(sqr(2))

g=lambda x:x*x

print(g(2))

l=[5,7,22,97,44]
lst=list(filter(lambda x: (x%2!=0),l))
print(lst)

lst=list(map(lambda x: x+2,l))
print(lst)

#sum=reduce((lambda x,y:x+y),l)

#print(sum)

###########################