FUNCTIONS
Function is a named block of statements used to carry out a well defined task.
FUNCTION
Function can be categorized into the following three types:
(i) Built-in function
(ii) Function defined in modules
(iii) User defined function
Built-in function: These are predefined function and are always available for use. For example
– len( ), type( ), int ( ), input ( ) etc.
Function defined in modules: A module in Python is a file that contains a collection of
related function. These are pre-defined in particular modules and can only be used when the
corresponding module is imported. E.g. math, random etc.
User defined function: A user-defined Python function is created or defined by the def
statement followed by the function name and parentheses as shown in the next slide.
A function is a programming block of codes which is used to perform a specific task
(computation). It only runs when it is called. It contains line of codes that are executed
sequentially from top to bottom by python interpreter. They are most important building
blocks for any software in python.
keyword Function
name
Header
def Sum(x):
total=0
for i in range(x):
indent
Function body
total=total+i
print ("The sum= ",total)
Sum(5) Function call
statement
Function Header: The first line of function definition that begins with keyword def
and ends with a colon (:), specifies the name of the function and its parameters.
Parameters: Variables that are listed within the parentheses of a function header.
Function Body: The block of statements / indented – statements beneath function
header that defines the action performed by the function. The function may or may
not return any value. A function returns a value through a return statement.
NOTE: Whenever a function call statement is encountered, an execution frame for
the called function is created and the control (program control) is transferred to it.
Arguments and Parameters:
The values being passed through a function-call statement are called
argument (or actual parameters or actual arguments).
The values received in the function definition/header are called
parameters (or formal parameters or formal arguments)
def f1(x,y):
print (x*y)
f1(20,30)
Here x,y are formal arguments whereas 20,30 are actual arguments.
Note: Return statement is used to return a value from the
function it returns None by default.
Default Parameter:- Python allows assigning a default
The return statement does the following:
value to the parameter. A default value is a value that is
• returns the control to the calling function.
predecided and assigned to the parameter when the
• return value(s) or None.
function call does not have its corresponding argument.
Default parameters
Fruitful vs Void function
Python has two types of functions:
fruitful – a fruitful function returns a result you
can use again. In above code p is positional argument
Void- While a void function does nothing after while r and t are keyword arguements
getting its job done.
Flow of Execution…
1 def greatest3(a,b,c):
6 if a>b and a>c:Parameters
7 return a
8 elif b>a and b>c:
9 return b
10 else:
11 return c
2 X=int(input(“Enter first number ”))
3 y=int(input(“Enter second number ”))
4 z=int(input(“Enter third number ”))
5 res=greatest(x,y,z) Arguements
12 Print(“Greatest of three numbers is :”,res)
Immutable Arguments:-
when integers, floats, strings or tubles are placed to the function as parameter that
is said to be immutable arguments. The changes made to these parameters are not
reflected back in their respective arguments.
Eg.
def change(n):
print(n)
n=n+10
print(n) Output:
a=15 15
print(a) 15
change(a) 25
print(a) 15
Mutable Arguments:-
when list or dictionary is passed to the function as parameter that is said to be
mutable arguments. The changes made to these parameters are reflected back in
their respective arguments.
Eg.
def change(L):
for i in range(len(L)):
L[i]=L[i]+5
print(L) Output:
A=[10,20,30] [10,20,30]
print(A) [15,25,35]
change(A) [15,25,35]
print(A)
Scope of Variables
The part of the program where a variable can be used is known as
scope of variable.
Two types of scopes:
Global scope
Local scope
Global Scope
With global scope variable can be used anywhere in the program.
Local Scope
With local scope variable can be used only within the function/ block that it is created.
a=10
global def update()
X=50 global a
a=15
def test(): local
print(a+10)
y=20 Update()
print()
print(‘value of x is’,x,’y is’,y)
print(‘value of x is’,x,’y is’,y) Output:
25
15
On execution the code will get value of x is 50, y is 20.
the next print statement will produce an error because the variable y is not
accessible outside the def().
Note: To access global variable inside the function as global prefix keyword global can be
used with the variable. Thus Variable can be used inside and outside of the function as well.