Wednesday, September 24, 2014

Python Getting Started : #2 Functions

This is the second in the queue which will be covering python functions in short.


A function is a named sequence of statements that performs a computation. one can call the function by its name.

>>> type('ABC')

<type 'str'>

Here the name of the function is type and expression in parenthesis is called the arguments of the function.

Type Conversion Functions

Various methods in python are used to convert values from one type to another like int, float, str, etc.

>>> int('40')


>>> int(11.99)


>>> float('3')


>>> str(40)


>>> str(3)


As it can be seen int function converts a float number to integers, instead of rounding it ignores the fractional part.

Math Functions

Python has a math module which consists most of the mathematical functions. A module is a file that contains a collection of functions. Before using any module, it has to be imported.

>>> import math

>>> print math

<module 'math' (built-in)>

import math statement creates a moduled object named math which has been printed above in second statement. This module object is the reference to the variables and functions defined in module. To access variable or function by module object, module object and function/variable name to be specified separated by a dot.

>>> 20 * math.pi


Expression math.pi is used to get the variable pi from math module.

Adding New Functions

>>> def new_function():

...         print 'I am in newly created new_function'


The name of the new function and sequence of statements form a function definition, this sequence of statements are executed when function is called. def is a python keyword which indicates to interpreter that this is a function definition. Rules for function names is same as for variables. You should avoid variable and function name as same. The first line of the function is called the header, rest is called body. The header has to be end with a colon and the body has to be indented. By convention indentation is to be always 4 spaces.

If a function definition is typed in an interactive mode, interpreter print ellipses(...) to let you know that definition is not complete, here to end the function you have to enter an empty line.

Defining a function creates a variable with same name.

>>> print new_function

<function new_function at 0xb79e7ee9c>

>>> type(new_function)

<type 'function'>

A defined function can be used inside other functions.

>>> def second_function():

...         print 'In second function'

...         new_function()


>>>  print second_function()

In second function

print 'I am in newly created new_function'

Remember you have to create a function before executing it means function definition is to be executed before the function is called the first time.

Flow of Execution

Order in which statements are executed is known as flow of execution. Execution always begins at the first statement. Statements are always executed once at a time, in order from top to bottom.

Function definitions does not affects the flow of execution as statements in the function definition are executed when that function is called.

When a function is called then control goes to body of that function and after executing all statements of called function it returns back where it left off. A function can be called from another function which may be called from some other function and so on, but we need not to worry as python is cool in tracking this. 

Parameter And Arguments

Inside a function, arguments are assigned to variables called parameters. The argument is evaluated before the function is called. Variables and parameters created in function are local, which means their scope is till that function only.

>>> str1 = 'First String'

>>> str2 = 'Second String'

>>> def add(var1, var2):

...         result = var1+var2


>>> print result

NameError: name 'result' is not defined

Void Functions

Functions which do not return any value when called are known as void functions. If you try to assign the result of the void function to a variable, you will get a special value called None. The value None is not same as string 'None', It is a sepacil value that has its own type.

>>> output = add('string1', 'string2')

>>> print output


>>> print type(ouput)

<type 'NoneType'>

Why to use functions

- Easy to debug in a program

- Can be reused.

- Eleminates a repititive code.

Importing with from

There are 2 ways to import modules. One already used above i.e import math, other is:

>>> from math import pi

>>> print pi


Now as you can see, you can access pi variable of math module directly without module name and dot notation. You can use * operator to import everything from module.

>>> from math import *

>>> cos(pi)


Add comment

Add comment