WORKING WITH FUNCTION
Function- Named subprogram that acts on data and often return value.
def sum(a, b):
c=a+b
print(c)
a=10
b=20
sum(a, b)
def means a function definition is starting.
Sum identifier following ‘def’ is the name of function
The variable or identifier inside the parentheses are the argument or parameter
ex. sum ( a, b):
there is a colon at the end of line , meaning it requires a block sum(a, b):
1. By default python named the segment with top- level statements (main program as)
__main__
2. Program execution begin with the first segment of __main__ segment.
Arguments(Actual Parameter)
Python refers to the values being passed in function call known as arguments.
Parameter(Formal Parameter)
Values being received in function header is known as parameter
1 Prepeared By- Mr. Rahul Tiwari (Vidya Devi Jindal School Kosi Kalan Mathura)
FUNCTION SCOPES
(LOCAL & GLOBAL SCOPE)
(HOW O RETURN MULTIPLE VALUES)
WORKING WITH FUNCTIONS
BASED
ON
CBSE BOARD EXAM
CLASS 12 Sci.
Prepared By -
RAHUL TIWARI
2 Prepeared By- Mr. Rahul Tiwari (Vidya Devi Jindal School Kosi Kalan Mathura)
SCOPES OF VARIABLE
Scope
Part(s) of program within which a name is legal and accessible, is called scope of the name.
GLOBAL SCOPE
A name declared in top level segment ( __main__ )of a program is said to have a global scope and is
usable inside the whole program and all blocks( functions, other block ) contained with in the program.
GLOBAL VARIABLE
A variable is a variable defined in the main program ( __main__ section). Such variable are said to be
global variables.
a and b can be used anywhere
def change( ): in program because there
print(a," ",b) scope is global
# __main__ section
a=15
b=20 global variables
change( )
GLOBAL ENVIRONMENT
print (a," ",b)
a=15
OUTPUT b=20
15 20
15 20
3 Prepeared By- Mr. Rahul Tiwari (Vidya Devi Jindal School Kosi Kalan Mathura)
LOCAL SCOPE
A name declared in function- body is said to have local scope i.e., it can be used only within this function
and the other blocks contained under it.
LOCAL VARIABLE
A local variable is a variable defined within a function .
a is Local Variable
With Local Scope
1. def change( ): Global Environment
2. a=100 Local Environment
3. print(a)
a=100
# __main__ section a=15
4. a=15
5. print(a)
6. change( )
7. print(a) a is global variable
with global scope
OUTPUT
15
100
15
4 Prepeared By- Mr. Rahul Tiwari (Vidya Devi Jindal School Kosi Kalan Mathura)
Example 1:
def change( x, y ):
# x and y are local variables Global Environment
x=10
y=20 a= 5 Local Environment For
print(x," ",y) change()
b=15
# __main__ section X= 5 10
# a and b are global variables
a=int(input("Enter value of a-")) Y=15 20
b=int(input("Enter value of b-"))
change(a,b)
print(a," ",b)
OUTPUT
Enter value of a-5
Enter value of b-15
10 20
5 15
Example 2:
def change( a, b ):
# a and b are local variables Global Environment
a=10
b=20 a= 5 Local Environment For
print(a," ",b) change()
b=15
# __main__ section a= 5 10
# a and b are global variables
a=int(input("Enter value of a-")) b=15 20
b=int(input("Enter value of b-"))
change(a,b)
print(a," ",b)
OUTPUT
Enter value of a-5
Enter value of b-15
10 20
5 15
5 Prepeared By- Mr. Rahul Tiwari (Vidya Devi Jindal School Kosi Kalan Mathura)
Example 3:
GLOBAL ENVIRONMENT
1. def calcsum( a, b, c): LOCAL ENVIRONMENT FOR average()
2. s=a+b+c
LOCAL ENVIRONMENT FOR
3. return s
calcsum()
num1=5
4. def average( x, y, z ): x=5 a=5
num2=10 b=10
5. sm=calcsum(x, y, z) y=10
num3=15 z=15 c=15
6. return sm /3
# __main__ section
7. num1=5
8. num2=10 global variables with global scope
9. num3=15
10. print( average(num1,num2,num3) )
OUTPUT
10 Flow Of Execution: 1- 4 – 7 – 8 – 9 – 10 – 4 – 5 – 1 – 2 – 3 – 5 – 6 – 10
6 Prepeared By- Mr. Rahul Tiwari (Vidya Devi Jindal School Kosi Kalan Mathura)
WHAT IF WE WANT TO USE THE GLOBAL VARIABLE INSIDE LOCAL SCOPE ?
Note- If you want to use the value of already created global variable inside a local function without
modifying it, then we can use simple.
Example-
def change( ):
print(a) #We use global variable a in Local Environment change( )
# __main__ section
a=15
print(a)
change( )
print(a)
OUTPUT
15
15
15
Note- But if you want to use global variable inside a inside a local scope and want assign some value
1 def change( ):
# a will be treat as a local variable in Local
Environment change( )
2 a=10
3 print(a)
# __main__ section
4 a=15 Global Environment
SOLUTION
5 print(a) Local Environment
6 change( ) a=15
change()
7 print(a)
OUTPUT a=10
15 Need to use global keyword before a if want to
10 assign value in local environment change( )
15
Flow Of Execution 1 – 4 – 5 – 6 – 2 – 3 – 7 1 def change( ):
2 global a
3 print(a)
1 def change( ): 4 a=10
2 print(a) #Global Variable 5 print(a)
3 a=10 #Error Global Environment
4 print(a) # __main__ section
6 a=15 a=15 10
# __main__ section Global Environment 7 print(a)
5 a=15 8 change( )
6 print(a) a=15 9 print(a)
7 change( )
8 print(a) OUTPUT
15
OUTPUT 15
15 10
UnboundLocalError: local variable 'a' referenced 10
before assignment
Flow Of Execution
Flow Of Execution
1 – 5 – 6 – 7 – 1 – 2 – 3 (Error)
1–6–7–8–1–2–3–4–5–9
7 Prepeared By- Mr. Rahul Tiwari (Vidya Devi Jindal School Kosi Kalan Mathura)
RETURNING MULTIPLE VALUES
Example 1:
def find(a,b):
return a+b,a-b,a*b #returning multiple values as tuple
a=int(input("Enter value for a-"))
b=int(input("Enter value for b-"))
c=find(a,b)
print(c) # c will print as tuple
OUTPUT
Enter value for a-7
Enter value for b-2
(9, 5, 14)
Example 2:
def find(a,b):
return a+b,a-b,a*b #returning multiple values as tuple
a=int(input("Enter value for a-"))
b=int(input("Enter value for b-"))
x,y,z=find(a,b) #Extract values from tuple
print(x,” “, y,” “,z)
OUTPUT
Enter value for a-7
Enter value for b-2
9 5 14
Example 3:
def find(a,b):
p= a+b
q=a-b
r=a*b
return p,q,r #returning multiple values as tuple
a=int(input("Enter value for a-"))
b=int(input("Enter value for b-"))
x,y,z=find(a,b) #Extract values from tuple
print(x,” “, y,” “,z)
OUTPUT
Enter value for a-7
Enter value for b-2
9 5 14
MISCELLANEOUS CONTENTS