CH-03 FUNCTIONS
Objectives •Why Functions
•Types of Functions
•To understand the details of function calls and parameter passing in
Python.
•To be able to define new functions in Python.
•To understand scope of formal parameters and variables defined in a
function.
Why •Code reuse
functions? •Easier to maintain (modularity)
•Facilitate solving a complex problem
•It reduces program size
•It makes program readable and understandable
Types of 1.Built-in:These are the functions whose functionality is pre defined in
Functions python like abs(), eval(), input() and many more.
2. Functions in modules: There are some predefined modules in python.
If we want to use a function residing in a module, we have to import
that module into our program. Example:
>>> import math
>>> print(math.sqrt(3))
3. User Defined functions
Functions in Module is a .pyfile which contains the definitions of functions and
modules variables.
Module is a simple python file.
When we divide a program into modules then each module
contains functions and variables. And each functions is made for a
special task.
Once written code in modules can be used in other programs.
When we make such functions which may be used in other
programs also then we write them in module.
We can import those module in any program and we can use the
KV2JHANSI/XII/CS/CH-03/WORKING WITH FUNCTION Page 1
functions.
We can import a module with the help of import statement. The
import statement can be used in two forms:
import <module> command: To import entire module
from < module> import <object> command: To import selected
object from a module.
>>> import math
>>> print(math.sqrt(3))
from math import sqrt
>>> print(sqrt(3))
KV2JHANSI/XII/CS/CH-03/WORKING WITH FUNCTION Page 2
KV2JHANSI/XII/CS/CH-03/WORKING WITH FUNCTION Page 3
User
defined
Functions
KV2JHANSI/XII/CS/CH-03/WORKING WITH FUNCTION Page 4
Structures def function1():
of Python :
Program :
def function2():
:
:
def function3():
:
:
#__main__ Python begins execution from here i.e. #__main__
Statement 1
Statement 2
KV2JHANSI/XII/CS/CH-03/WORKING WITH FUNCTION Page 5
Flow of
execution
in a
function
call
Argument Arguments are values appear in function call.
and Parameters are values appear in function header.
Parameters # Program to add two numbers
def sum(a,b): # a,b are formal parameter
c=a+b
return c
n1=int(input(“Enter value”)) # n1,n2 are actual arguments
n2= int(input(“Enter value”))
res=sum(n1,n2)
print(“Sum=“,res)
Types of Python supports four types of formal parameters.
Parameters 1.Positional arguments
2.Defaults arguments
3.Keywords arguments
4.Variable Arguments
Positional The function call statement must match the number and order of
Arguments arguments as defined in the function definition, this is called the
Positional argument matching.
In this no value can be skipped from function call or you can’t change
the order of arguments also
KV2JHANSI/XII/CS/CH-03/WORKING WITH FUNCTION Page 6
If a function definition header is like.
def check(a,b,c):
:
then possible function calls for this can be:
check(x,y,z)#a gets value x, b gets value y, c gets value z
check(2,x,y) #a gets value 2, b gets value x, c gets value y
check(2,5,7) #a gets value 2, b gets value 5, c gets value 7
Default A parameter having default value in the function header is known
Arguments as a default parameter.
Example of function header with default values:
def interest ( principle, time, rate=0.10):
# 0.10 is default value for parameter rate. if in a function call, the
value for rate is not provided, Python will fill the missing value
with this value.
if a function calls appears like:
si=interest(5400,2)
#third argument missing, its default value 0.10 is used for rate
si=interest(6100,3,0.15)
# no argument missing with principle=6100 , time=3, rate=0.15
Following are examples of header with default values:
def interest (p,t,r=0.5):#correct
def interest(p,t=2, r):# incorrect
Any parameter can’t have a default vale unless all parameters
appearing on its right have their default values.
Keyword Keyword arguments are the named arguments with assigned values
Arguments being passed in function call statement.
Python offers a way of writing function calls where you can write any
argument in any order provided you name the arguments when calling
the function.
1.interest (p=2000,time=2, r=0.10)
# p gets value 2000, t gets value 2, r gets value 0.10
2.interest ( t=4,p= 2600, r=0.09)
KV2JHANSI/XII/CS/CH-03/WORKING WITH FUNCTION Page 7
#p gets value 2600, t gets value 4, r gets value 0.09
3.interest (t=2,r=0.12, p=2000)
#p gets value 2000, t gets value 2, r gets value 0.12
SCOPE OF VARIABLE
Local and 1.def sum(x,y):
Global 2 z=x+y
Variable 3. return z
# Main Programme
4.n1=int(input(“Enter n1”)
5.n2=int(input(“Enter n2”)
6.s=sum(n1,n2)
7.print(s)
Name Resolution (Resolving Scope of a name)
LEGB RULE- L-Local Environment-
LEG E-Enclosing Environment
B G-Global Environment
B-Built in Environment
# Case 1-Variable in Global Scope but not in local scope
def calsum(x,y):
s=x+y
print(n1)
return s
# main Programme
n1=int(input("Enter n1"))
n2=int(input("Enter n2"))
print(calsum(n1,n2))
OUTPUT
Enter n120
Enter n240
20
60
======================================================================
KV2JHANSI/XII/CS/CH-03/WORKING WITH FUNCTION Page 8
# Case 2-Variable neither in Global Scope nor in local scope
def show():
print(n3)
# main Programme
print(show())
OUTPUT
Error
=======================================================================
# Case 3- Some Variable name in local as well as in global scope
def run():
s=15 # local scope
print(s)
# main Programme
s=12 # Global Scope
print(s)
run()
print(s)
print("================================")
OUTPUT
12
15
12
=====================================================================
# case 4- If you want to use global variable insisde local scope
def run():
global s
s=90
print(s)
# main Programme
s=12 # Global Scope
print(s)
run()
print(s)
print(s)
KV2JHANSI/XII/CS/CH-03/WORKING WITH FUNCTION Page 9
OUTPUT
12
90
90
90
Practice # Q1.What is the output of the following code ?
Question
a=1
def fun():
a=10
print(a)
# main programe
fun()
print(a)
OUTPUT.
10
1
====================================================================
# Q2.What is the output of the following code ?
a=1
def fun():
a=10
return a
# main programe
print(a)
print(fun())
print(a)
OUTPUT
1
10
1
# Q3.What is the output of the following code ?
a=1
def fun():
global a
a=50
return a
KV2JHANSI/XII/CS/CH-03/WORKING WITH FUNCTION Page 10
# main programe
print(a)
print(fun())
print(a)
OUTPUT
1
50
50
MUTABLE/IMMUTABLE PROPERTIES OF PASSED DATA OBJECT
Depending upon the mutability / immutability of its data type of variable behave
differently that is if a variable is referring to an immutable data type then any
change in its value will also change the memory address it is referring to ,but if a
variable is referring to mutable type then any change in the value of mutable type
will not change the memory address of the variable.
KV2JHANSI/XII/CS/CH-03/WORKING WITH FUNCTION Page 11
KV2JHANSI/XII/CS/CH-03/WORKING WITH FUNCTION Page 12
KV2JHANSI/XII/CS/CH-03/WORKING WITH FUNCTION Page 13
KV2JHANSI/XII/CS/CH-03/WORKING WITH FUNCTION Page 14
KV2JHANSI/XII/CS/CH-03/WORKING WITH FUNCTION Page 15
KV2JHANSI/XII/CS/CH-03/WORKING WITH FUNCTION Page 16
KV2JHANSI/XII/CS/CH-03/WORKING WITH FUNCTION Page 17
KV2JHANSI/XII/CS/CH-03/WORKING WITH FUNCTION Page 18
KV2JHANSI/XII/CS/CH-03/WORKING WITH FUNCTION Page 19
KV2JHANSI/XII/CS/CH-03/WORKING WITH FUNCTION Page 20
KV2JHANSI/XII/CS/CH-03/WORKING WITH FUNCTION Page 21