More On Functions (23!12!2022)
More On Functions (23!12!2022)
In [ ]: Functional Programming :
Two Types of Variables are there:
1.Local Variables
2.Global Variable
Global Variable
In [ ]: Global Variable--> are those variable that are declared and intilized outside any function.
Example
In [1]: a=10 #Global Variable
def check():
print(a)
return ""
print(check())
10
Local Variable
In [ ]: Local Variable --> are those variable that are declared and initilized inside the function
Example
In [2]: def check():
a=10 #Local Variable
b=20 #local Variable
print(a)
print(b)
return ""
print(check())
10
20
a.Global variable --> global variables can be accessed any where in the program.
(either outside or inside)
b.Local variable can only be accessed within the function.You cannot access local variable
outside
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [3], in <cell line: 5>()
3 bd=20 #local Variable
4 return ""
----> 5 print(abc)
def checking():
print("Inside second function",abc)
print(abc)
return ""
print("Outside Function ",abc)
print(check())
print(checking())
print(abc)
Note --> global variable can be assessed anywhere in the program.The scope of global variable is
within a program.
200
--> If local and global variable name are same then PVM will check the variable first in local scope
if the variable is present at local scope then that variable will be printed and if the variable
is not present in local scope then it will check at global scope if the variable is present at
global scope that that will be printed if the variable is not present in global as well as
local then error will be there
1000
20
10
Global keyword
In [ ]: Global Keyword is used to declare global variable inside the function.
Example
In [9]: az=10
def f1():
print(az)
return ""
def f2():
global az
az=777
print(az)
return ""
print(f2())
print(f1())
777
777
In [10]: abc=999
def f1():
print(abc+100)
return ""
def f2():
global abc
abc=900
print(abc+800)
return ""
def f3():
print(abc)
return ""
print(f1())
print(f3())
1099
999
Nested Functions
In [ ]: --> Nested Functions are also possible in Python
--> Function inside the function is known as Nested Function.
--> Inner function will be executed first.then outer function
--> You cannot call inner function directly
--> Inner function is always local to the outer function
Example
In [11]: def add(x,y):
def sub(x,y):
return x-y #-10
return sub(10,20) #return -10
print(add(10,20))
-10
print(outer())
Inner function
print(outer())
Outer Function
Inner function
(-10, 30)
-5
3000
Outer function
Outer add function
45
Note --> Inside a function after return statement nothing will be executed. No any statement after return
statement will be considered
Recursive Function
In [ ]: Recursive Function --> When a function call itself then such types of funcitons are known as
recursive function
In [ ]: factorial(3):
3*factorial(2)
3*2*factorial(1)
3*2*1*factorial(0)
3*2*1*1
3*2*1*1
6
Example
In [18]: def factorial(num):
if num==0:
return 1
return num*factorial(num-1)
print(factorial(5))
120