Function_part2
Function_part2
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)
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)