In this tutorial section we will learn Nesting of Function in C programming Language.
In the C programming language, functions are organised block of codes that performs specific task and promote reusability.
Nested Function: A function defined inside another function is known as a nested function in C programming language.
The main() function is the starting point of any C program where the program's execution starts..
Till now we have declared the main() function And have called the other predefined and user defined fucntion inside it like printf(),scanf(),getc(),putc() etc.
So that the main function can be know as a nested function.
Nested functions in c programming languge assume block scope or function scope.The scope of a nested function is inside the enclosing function.
A nested function can access other variables,types,local functions, classes, constants, etc.
Let’s understand the nested function from the following diagram and example:
Function Nesting in C Language
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.
Other Topics:
Variables and Identifiers
Relational Operators
if-else statements
Switch case
While Loop
Infinite while Loops
C FOR Loop
Infinite for Loops
Continue in Loops
One Dimensional Array
Two Dimensional Arrays
Read and Display 2D Arrays
Types of functions
Passing Array To Functions
Nesting of Function
Array vs Structure
Array of Structure
Structures and Functions
Structures Within Structures
Use Of Pointers In C
File Handling In C
Loops FAQ
Arrays FAQ
count vowels in a file
Function FAQ
Conditional Statements Assignments
For Loops Assignments
Arrays Assignments
Function Assignments
Structure Assignments
Pointers Assignments
Files Assignments
Storage classes Assignments
Binary Files
count words,lines in a file
Copy files
Update File
Continue in Loops
break in Loops
Difference Between While and Do while
difference while do..while & for
malloc
calloc
Storage Classes
Operators MCQ
Conditional Statements MCQ
Loops MCQ
Arrays MCQ
Function MCQ
Structure MCQ
Pointers MCQ
Files MCQ
Storage classes MCQ