Download as pdf or txt
Download as pdf or txt
You are on page 1of 25

FUNCTIONAL PROGRAMMING IN PYTHON

FUNCTIONS
Contents:

• Global, Local and Nonlocal variables


• Nested Functions
Global, Local and Nonlocal variables

Global Variables
In Python, a variable declared outside of the function or in global scope is known as a
global variable. This means that a global variable can be accessed inside or outside of
the function.

Example 1: Create a Global Variable


x = "global"

def foo():
Output
print("x inside:", x)
x inside: global
foo()
x outside: global
print("x outside:", x)
In the above code, we created x as a global variable and defined a foo() to print the
global variable x. Finally, we call the foo() which will print the value of x.

What if you want to change the value of x inside a function?


x = "global"

def foo(): The output shows an error because Python


x=x*2 treats x as a local variable and x is also not
print(x) defined inside foo(). To make this work, we use
the global keyword.
foo()
Output
UnboundLocalError: local variable 'x' referenced before assignment
x = "global"

def foo():
x="abcd"
x=x*2
print(x)

foo()

Output:
abcdabcd
x = "global"

def foo():
print(x*2)

foo()

Output:
globalglobal
Local Variables
A variable declared inside the function's body or in the local scope is known as a local
variable.

Example 2: Accessing local variable outside the scope


def foo():
y = "local"
foo()
print(y)
Output
NameError: name 'y' is not defined

The output shows an error because we are trying to access a local variable y in a global
scope whereas the local variable only works inside foo() or local scope.
Example 3: Create a Local Variable
Normally, we declare a variable inside the function to create a local variable.

def foo():
y = "local"
print(y)

foo()

Output

local
Example 4: Using Global and Local variables in the same code
x = "global "
def foo():
global x
y = "local"
x=x*2
print(x) The global Keyword
If we want those changes to be reflected in our global
print(y)
variable, instead of making a new local one, all we
have to do is add the global keyword.
foo()

Output

global global
local
In the above code, we declare x as a global and y as a local variable in the foo(). Then, we
use multiplication operator * to modify the global variable x and we print both x and y.

After calling the foo(), the value of x becomes global global because we used the x * 2 to
print two times global. After that, we print the value of local variable y i.e local.
Example 5: Global variable and Local variable with same name
x=5

def foo():
x = 10
print("local x:", x)

foo()
print("global x:", x)

Output

local x: 10
global x: 5
In the above code, we used the same name x for both global variable and local variable.
We get a different result when we print the same variable because the variable is
declared in both scopes, i.e. the local scope inside foo() and global scope outside foo().

When we print the variable inside foo() it outputs local x: 10. This is called the local
scope of the variable.

Similarly, when we print the variable outside the foo(), it outputs global x: 5. This is
called the global scope of the variable.
Python Nested Functions
Like nested loops, we can also have nested functions in Python. We simply create a function
using def inside another function to nest two functions.
A function defined inside another function is called a nested function. Nested functions
can access variables of the enclosing scope. these non-local variables are read-only by
default and we must declare them explicitly as non-local (using nonlocal keyword) in order
to modify them.
For example,

def f1(): #outer function


print ("Hello")
def f2(): #inner function
Output
print ("world")
f2()
Hello
f1()
world
def print_msg(msg):
# This is the outer enclosing function

def printer():
# This is the nested function
print(msg)

printer()

# We execute the function


We can see that the nested printer() function is
# Output: Hello
able to access the non-local msg variable of
print_msg("Hello")
the enclosing function.
Output

Hello
def f1(): #outer function
x = 1 #variable defined in f1 function
def f2(a): #inner function
print (a+x) #able to acces the variable of outer function
f2(2)

f1()

Output

3
It is important to mention that the outer function has to be called in order for the
inner function to execute. If the outer function is not called, the inner function will
never execute.

def function1(): # outer function


print ("Hello from outer function")
def function2(): # inner function
print ("Hello from inner function")

function2()

The code will return nothing when executed!


This seems quite simple. You can see that we also accessed the variables of the outer
function from the inner function. So, let's try to change the variables of the outer
function from the inner one.
def f1(): #outer function
x = 1 # variable defined in the outer function
def f2(a): #inner function
#will create a new variable in the inner function
#instead of changing the value of x of outer function
x=4 Output
print (a+x)
print (x) # prints the value of x of outer function 1
f2(2) 6
f1()

From the above two examples, you can see that we are able to print the variables of the
outer function from the inner one but not able to change it.
Why use Inner Functions?

Encapsulation
A function can be created as an inner function in order to protect it from everything
that is happening outside of the function.
Nonlocal Variables

nonlocal is a keyword (case-sensitive) in python, it is used when we work with the


nested functions and we need to use a function which is declared in outer function,
if we do the same, a variable will be created as local and we then we will not be able
to work with a variable in inner function which is declared in outer function.

In such a case, we can define the variable (which is declared in outer function) as a
nonlocal variable in inner function using nonlocal keyword.
Example:

def outerfunc():
a = 10
def innerfunc():
# nonlocal binding
nonlocal a
a = 100

# calling inner function


innerfunc()
# printing the value of a
print("a : ", a)

Output:
a : 100
# python code to demonstrate an example of nonlocal keyword in nested functions
def outerfunc():
a = 10
b = 20

def innerfunc():
# nonlocal binding
nonlocal a
a = 100 # will update
b = 200 # will not update, it will be considered as a local variable

# calling inner function


innerfunc()
# printing the value of a and b
print("a : ", a) Output
print("b : ", b)
a : 100
# main code calling the function i.e. outerfunc()
outerfunc()
b : 20
As you see in the output, a and b are the variables of outerfunc() and in the
innerfunc() we are binding variable a as local, thus a will not be local here but, b will
be considered as local variable for innerfunc() and if we change the value of b that will
be considered a new assigned for local variable(for innerfunc()) b.
THANK YOU

You might also like