Function Nesting in C language.Function Defined inside another function is called nesting of function.

  Functions Nesting in C Programming Language.
Note: i.Given diagram just demonstrates the nesting functions in C programming.
ii.We can not define a function within another function in C language. Only declaration of a function is possible inside another function.
Lets discuss about Nesting of function in C programming language from given diagram.
Nested function can not be defined in C programming and actually not allowed .When one or more functions are used under a specific function, it is known as nesting function in C Programming Language.
Lets understand how function can be nested from the given diagram.

Function Nesting in C Language.

return_type first_function()
 {
  //statements
  second_function();
 }
return_type second_function()
 {
  //statements
  third_function();
 }
return_type third_function()
 {
  // statements
 }
int main()
 {
  first_function();
  return(0);
 }

Explanation:  In the above example, we have defined and declared three functions first_function(), second_function() and third function respectively. The main() function is calling the first_function() , So that we can say that the main() is a nested function. Also, in the definition of first_function(), it is calling another function second_function(). So, the function first_function() is also a nested function.
One thing we need to keep in mind that "The code or programs containing nested functions, the outer or enclosing function returns back only when all the inner functions complete their task".

C program to demonstrate use of Nested function in C.

The following example given is a simple implementation of nested functions in C Programming that calculates the cube of a number.

#include <stdio.h>
int num_cube(int num)
 {
  int cal_cube()
  {
   return num * num*num;
   }
  return cal_cube();
}
int main()
 {
  int x, ans;
  printf("Enter a number: ");
  scanf("%d", &x);
   ans = num_cube(x);
   printf("The cube of %d is %d.\n", x, ans);
   return 0;
 }

Output:
Enter a Number:
5.
The Cube of 5 is 125.

In the example provided above , the nested function behavior is achieved by defining a function (cal_cube) inside another function (num_cube). Although it seems to be a nested function, it is actually a local function defined within the scope of num_cube(). It is not a true nested function as supported by some other programming languages.

Previous Topic:-->> Types of Functions in C || Next topic:-->>Recursion Function in C.