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

More On Function

The document discusses global and local variables in Python functions. It defines global variables as those declared outside a function and accessible within any function. Local variables are declared within a function and only accessible within that function. The global keyword can be used to declare a global variable within a function or make a global variable accessible to a function to modify it. The document also provides an example of a recursive function to calculate the factorial of a number.

Uploaded by

aasthaa1805
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)
16 views

More On Function

The document discusses global and local variables in Python functions. It defines global variables as those declared outside a function and accessible within any function. Local variables are declared within a function and only accessible within that function. The global keyword can be used to declare a global variable within a function or make a global variable accessible to a function to modify it. The document also provides an example of a recursive function to calculate the factorial of a number.

Uploaded by

aasthaa1805
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/ 3

In [1]: #1.

Global Variables
'''The variables which are declared outside of function are called global variables
These variables can be accessed in all functions of that module.'''

Out[1]: 'The variables which are declared outside of function are called global variable
s.\nThese variables can be accessed in all functions of that module.'

In [2]: a=10 #global variable


def f1():
print(a)
#####
def f2():
print(a)
#########
f1()
f2()

10
10

In [3]: #2. Local Variables:


'''The variables which are declared inside a function are called local variables.
Local variables are available only for the function in which we declared it.
i.e from outside of function we cannot access.'''
###########
def f1():
b=10 #local variable
print(b)
#####
def f2():
print(b)
#########
f1()
f2()

10
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[3], line 14
12 #########
13 f1()
---> 14 f2()

Cell In[3], line 11, in f2()


10 def f2():
---> 11 print(b)

NameError: name 'b' is not defined

In [4]: #global keyword:


'''We can use global keyword for the following 2 purposes:
1. To declare global variable inside function
2. To make global variable available to the function so that we can perform require
modifications'''
############
c=10
def f1():
c=777 #local variable
print(c)
#####
def f2():
print(c)
#########
f1()
f2()

777
10

In [5]: d=10
def f1():
global d
d=777 #local variable
print(d)
#####
def f2():
print(d)
#########
f1()
f2()

777
777

In [6]: def f1():


global e
e=100 #local variable
print(e)
#####
def f2():
print(e)
#########
f1()
f2()

100
100

In [7]: '''Note: If global variable and local variable having the same name then we can
access global variable inside a function as follows'''
###############
g=110
def f1():
g=100 #local variable
print(g)
print(globals()['g'])
######
f1()

100
110
In [8]: #Recursive function:
#Write a Python Function to find factorial of given number with recursion.
###########
def factorial(n):
if n==0:
result=1
else:
result=n*factorial(n-1)
return result
##################
print(f'factorial of {4} is: {factorial(4)}')
print(f'factorial of {5} is: {factorial(5)}')

factorial of 4 is: 24
factorial of 5 is: 120

In [ ]:

You might also like