# Implementation of nested function in Python

Hey guys…
In this tutorial, we will discuss the nested function in Python. The nested type function is a very useful concept of the topic of function in a programming world.

First of all, we will know about what is a nested function. A function that is declared inside the function is known as the nested function. A nested function is able to access the variable and the predefined and user-defined function in the nested function.here is an example of nested function.

```def outerfun():                         #outer function
print("outer functon execution")
def innerfun():                       #inner function
print("execution of inner functon")
print("outer functon calling inner function")
innerfun()                            #calling of inner function
outerfun()                              #calling of outer function
#innerfun()===>NameError:'namefun' is not defined```

In the above example, we have taken two functions called outerfun and innerfun the inner function is defined into the outer function as a local variable of the outer function. That means we can not directly access the inner function out of the scope of the outer function.

In the above example, we have tried to call the inner function outside the scope of outer function but it encountered an error called NemeError which means the name of function we are calling is not defined in the outer function.

Let’s make a program for a better understanding of the nested function

#### Program for finding the area of the rectangle and the triangle

```def area():
print("finding the area of the diffrent shapes")
def triangle(r):
tri=3.14*r**2
print("the area of triangle is",tri)
def square(a):
squ=a**2
print("area of the square is ",squ)
triangle(3)
square(5)
area()```

In the above program, there is a function called area and there are two subfunctions one is a triangle and another is square. We are calculating the area of the triangle and square with the help of the given formula.

For running the internal functions we call all the functions inside the main function like triangle() and the square() in the square function.

#### The output of the above program:

```finding the area of the different shapes
area of the triangle is 28.26
area of the square is  25```

Related article from this site that you may like:

I hope you have got the idea of nested function in Python and how to use it in the practical world.