You are on page 1of 4

A function consists of a group of statements that perform specific task.

There are built in functions in Python to perform various tasks. For example, to
display output, Python has print ( ) function. Programmer can also create his
own functions which are called user defined functions.
Advantages of functions:
1.Once a function is written it can be used as and when required. So functions
are also called reusable code. Because of this reusability, the programmer can
avoid code redundancy. It means it is possible to avoid writing the same code
again and again.
2.Functions provide modularity for programming. Usually, a programmer
divides the main task into smaller subtasks called modules. To represent each
module, the programmer will develop a separate function.
3. Code maintenance will become easy because of functions. When a new
feature has to be added to the existing software, a new function can be written
and integrated into the software. Similarly, when a particular feature is no
more needed by the user the corresponding function can be deleted or put
into comments.
4. By using functions debugging will be come easy.
5. The use of functions in a program will reduce the length of the program.
Defining a function
A function is defined using the keyword def followed by function name. After
the function name parameters are written within parenthesis.
For example
def sum(a,b):
Here def represents the starting of the function definition. sum is the name of
the function. After this name parenthesis are compulsory as they denote that it
is a function and not a variable. Within parenthesis there are 2 variables a and
b which are called parameters. Parameter is a variable that receives data into
the function. After parenthesis, we put a colon( : ) that represents the
beginning of the function body. The function body contains a group of
statements.
Calling a function
A function executes when it is called. A function is called using its name. While
calling the function, we should pass the necessary values to the function in the
parenthesis as sum (10, 15). When this statement is executed the Python
interpreter jumps to the function definition and copies the values 10 and 15
into the parameters a and b respectively. These values are processed in the
function body and the result is obtained. The values passed to a function are
called arguments. So 10 and 15 are arguments.
Returning results from a function:
We can return the result from the function using a return statement in the
body of the function.
When a function does not return in any result, we need not write the return
statement in the body of the function. Now we will write sum function such
that it will return the sum value rather than displaying it.
def sum(a,b):
c=a+b
return c
#call the function
x=sum(10,15)
print(“The sum is “,x)
y=sum(1.5,10.75)
print(“The sum is “,y)

Formal and actual parameters


When a function is defined,it may have some parameters . These parameters
are useful to receive values from outside of the function. They are called
formal parameters. When we call the function, we should pass data or values
to the function. These values are called actual parameters.
--------------------------------------------------------------------------------------------------------
Scope of the variable-part of the program in which variable is accessible is
called its scope.
Lifetime of the variable-duration for which the variable exists is called its
lifetime.
Local and global variables
When we declare a variable inside a function, it becomes a local variable. A
local variable is a variable whose scope is limited only to that function where it
is created. That means the local variable value is available only in that function
and not outside that function. In the following example the variable a is
declared inside myfunction () and hence it is available inside that function.
Example
def myfunction( ):
a=1 #this is local variable
a+=1
print( a)
myfunction( )
print(a) #error, not available

Global variables are visible throughout the program.


Example
a=1 # this is global variable
def myfunction( ):
b=2 #this is local variable
print( ‘a=’,a) #display global var
print( ‘b=’,b) #display local var
myfunction( )
print(a) #available
print(b) #error, not available
Whereas the scope of local variable is limited only to the function where it is
declared, the scope of global variable is the entire program body written below
it.

lGlobal Variables Local Variables

They are defined in the main body of the 1. They are defined within a function and is local to
rprogram file that function

They can be accessed throughout the program 2. They can be accessed from the point of its
file
definition until the end of the block in which it is
defined

Global variables are accessible to all functions in 3. They are not related in any way to other variables
hthe program with the same names used outside the function.

You might also like