Functional Programming
Functional Programming
FUNCTIONS
Contents:
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.
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.
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.
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 printer():
# This is the nested function
print(msg)
printer()
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.
function2()
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
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
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