0% found this document useful (0 votes)
3 views

Function_part2

The document explains the concept of variable scope in programming, distinguishing between global and local variables. Global variables are accessible throughout the program, while local variables are confined to the function or block where they are defined. Examples illustrate how local variables cannot be accessed outside their respective functions, leading to errors when attempting to do so.

Uploaded by

MriNmoy Paul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Function_part2

The document explains the concept of variable scope in programming, distinguishing between global and local variables. Global variables are accessible throughout the program, while local variables are confined to the function or block where they are defined. Examples illustrate how local variables cannot be accessed outside their respective functions, leading to errors when attempting to do so.

Uploaded by

MriNmoy Paul
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

FUNCTION

Scope of Variable
The part of the program where a variable is accessible is defined as the scope of that variable.
A variable can have one of the two scopes: A variable that has global scope is known as a
global variable and a variable that has a local scope is known as a local variable.
(A) Global Variable A variable that is defined outside all the functions or all the blocks is
known as a global variable. It can be accessed in any functions defined onwards. Any change
made to the global variable will impact all the functions in the program where that variable can
be accessed.
(B) Local Variable A variable that is defined inside any function or a block is known as a local
variable. It can be accessed only in the function or a block where it is defined. It exists only till
the function executes.

Program Output
def fn1(i,j): Enter 1st number:45
s=i+j Enter 2nd number:5
return s Sum= 50
def fn2(k,m): Product= 225
p=k*m Traceback (most recent call last):
return p File
x=int(input("Enter 1st "C:/Users/User/AppData/Local/Programs/Python/Python311/functi
number:")) on.py", line 11, in <module>
y=int(input("Enter 2nd print(i)
number:")) NameError: name 'i' is not defined. Did you mean: 'id'?
print("Sum=",fn1(x,y))
print("Product=",fn2(x,
y))
print(i)

In the above program,


x and y are global variables.
i, j and s are local variables of function fn1. k, m and p are local variables of function fn2.
The last line of the program is producing an error because there is no global variable named i in
the program and the local variable i of function fn1 is not accessible outside the function fn1.
Note
• Any modification to global variable is permanent and affects all the functions where it is used.
• If a variable with the same name as the global variable is defined inside a function, then it is
considered local to that function and hides the global variable.
• If the modified value of a global variable is to be used outside the function, then the keyword
global should be prefixed to the variable name in the function.
Program Output
def fn(): 10
n=20 20
print(n) 10

n=10
print(n)
fn()
print(n)
def fn(): 10
global n 20
n=20 20
print(n)

n=10
print(n)
fn()
print(n)

You might also like