”;
The term nesting, in programming context refers to enclosing a particular programming element inside another similar element. Just like nested loops, nested structures, etc., a nested function is a term used to describe the use of one or more functions inside another function.
What is Lexical Scoping?
In C language, defining a function inside another one is not possible. In short, nested functions are not supported in C. A function may only be declared (not defined) within another function.
When a function is declared inside another function, it is called lexical scoping. Lexical scoping is not valid in C because the compiler cannot reach the correct memory location of inner function.
Nested Functions Have Limited Use
Nested function definitions cannot access local variables of surrounding blocks. They can access only global variables. In C, there are two nested scopes: local and global. So, nested functions have limited use.
Example: Nested Function
If you want to create a nested function like the one shown below, then it will generate an error −
#include <stdio.h> int main(void){ printf("Main Function"); int my_fun(){ printf("my_fun function"); // Nested Function int nested(){ printf("This is a nested function."); } } nested(); }
Output
On running this code, you will get an error −
main.c:(.text+0x3d): undefined reference to `nested'' collect2: error: ld returned 1 exit status
Trampolines for Nested Functions
Nested functions are supported as an extension in “GNU C”. GCC implements taking the address of a nested function using a technique called trampolines.
A trampoline is a piece of code created at runtime when the address of a nested function is taken. It requires the function to be prefixed with the keyword auto in the declaration.
Example 1
Take a look at the following example −
#include <stdio.h> int main(){ auto int nested(); nested(); printf("In Main Function nown"); int nested(){ printf("In the nested function nown"); } printf("End of the program"); }
Output
When you run this code, it will produce the following output −
In the nested function now In Main Function now End of the program
Example 2
In thi program, a function square() is nested inside another function myfunction(). The nested function is declared with the auto keyword.
#include <stdio.h> #include <math.h> double myfunction (double a, double b); int main(){ double x = 4, y = 5; printf("Addition of squares of %f and %f = %f", x, y, myfunction(x, y)); return 0; } double myfunction (double a, double b){ auto double square (double c) { return pow(c,2); } return square (a) + square (b); }
Output
Run the code and check its output −
Addition of squares of 4.000000 and 5.000000 = 41.000000
Nested Functions: Points to Note
One needs to be aware of the following points while using nested functions −
- A nested function can access all the identifiers of the containing function that precede its definition.
- A nested function must not be called before the containing function exits.
- A nested function cannot use a goto statement to jump to a label in the containing function.
- Nested function definitions are permitted within functions in any block, mixed with the other declarations and statements in the block.
- If you try to call a nested function through its address after the containing function exits, it throws an error.
- A nested function always has no linkage. Declaring one with “extern” or “static” always produces errors.
”;