Functions Day4
Functions Day4
SCOPE OF
VARIABLES
What is Variable Scope?
Parts of Program within which
a variable is accessible, is
known Variable Scope.
TYPES OF VARIABLE SCOPE
There are broadly two kinds
of scopes in python-
1.LOCAL SCOPE
2.GLOBAL SCOPE
LOCAL SCOPE
• Variables declared in a function body is said to have Local Scope i.e it can
be used only within this function.
They are destroyed once we return from the function. Hence, a function
does not remember the value of a variable from its previous calls.
LOCAL SCOPE
Example:
OUTPUT:
In the above code, x is local to my_func() ,hence accessible inside the function
only.if we try to print its value outside function “NameError” will come.
GLOBAL SCOPE
• A Variable declared in top level segment(_main_)of a program is said to have a
Global Scope.
• A Global Variable is accessible inside the whole program and all
blocks(functions, other block of codes) contained in the program.
• Lifetime of a Global Variable is the period throughout which the variable exists
in the memory.
Example:
def my_func():
x = 10
OUTPUT:
print("Value inside function:",x) Value inside function: 10
x = 20 Value outside function: 20
my_func()
print("Value outside function:",x)
Here,
we can see that the value of x is 20 initially.
Even though the function my_func()changed the value of x to
10,
it did not affect the value outside the function. This is
because the variable x inside the function is different (local
to the function) from the one outside. Although they have
same names, they are two different variables with different
scope.
Use of “global” keyword
Example:
def my_func():
global x
print("Value inside function:",x)
x = 20
my_func()
print("Value outside function:",x)
Output:
Value inside function: 20
Value outside function: 20
IMMUTABLE ARGUMENTS
(passing immutable type value to a function)
Example:
def my_func(n):
n=n+2
print("Value inside function:",n)
x = 20
my_func(x)
print("Value outside function:",x)
Output:
Value inside function: 22
Value outside function: 20
So in the above example
the function my_func() received the passed value in parameter n and then changed the value of n to 22 but after returning from my_func(), the
originally passed variable remains unchanged as it is immutable. Lets understand how,
20
Local envir.
x n
Global Environment
Inside the function, my_func() , n is now refer to 22 but the value of
x is still referring to 20 .Hence Value of x remain unchanged.
20 22
Local envir.
x n
Global Environment
MUTABLE ARGUMENTS
(Passing Mutable type value to
function)
Example:
def increment(n):
n.append([4])
L = [1, 2, 3]
increment(L)
print(L)
Output
L = [1, 2, 3, 4]
The code L = [1, 2, 3] has the variable L refer to the list object
containing references to three immutable objects: integer 1, 2, and 3
When we pass L to
increment() , local
variable n refer to
same object.
The .append() method change the value in place as List is
mutable.
def check(n1=1,n2=2):
n1=n1+n2
n2+=1
print(n1,n2)
check()
check(2,1)
check(3)
OUTPUT
33
32
53
Find and write the output of the following snippet:
def interest(prnc,time=2,rate=0.10):
return(prnc*time*rate)
print(interest(6100,1))
print(interest(5000,rate=0.05))
print(interest(5000,3,0.12))
print(interest(time=4,prnc=5000))
OUTPUT
610.0
500.0
1800.0
2000.0