Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are nested functions part of C standard?

Tags:

c

Today i came across nested functions which i had never heard of. Is it only part of GNU C?

Here is a wikipedia example of nested function.

float E(float x)
{
    float F(float y)
    {
        return x + y;
    }
    return F(3);
}

From the code, it looks like nested functions are sort of C++ inline functions. So, is it possible to take out the address of nested function?

Edit:

The gcc link given by Adam says that nested function's code is created dynamically on stack. But how do you run code from stack? Shouldn't it be there in code segment.

like image 698
chappar Avatar asked Mar 20 '09 15:03

chappar


People also ask

How C programming supports nested user defined functions?

A nested function is a function defined inside the definition of another function. It can be defined wherever a variable declaration is permitted, which allows nested functions within nested functions. Within the containing function, the nested function can be declared prior to being defined by using the auto keyword.

What is the point of nested functions?

Nested (or inner, nested) functions are functions that we define inside other functions to directly access the variables and names defined in the enclosing function. Nested functions have many uses, primarily for creating closures and decorators.

What do you mean by nested function?

Information can be added to a function's memory block only through a function's input variables. Information can leave the function's memory block only through a function's output variables. 3. A function can be defined within another function, which is called a nested function.


1 Answers

No, they are not part of the C or C++ standard. They are a GNU extension in the GCC compiler. See the GCC manual for more information. It is actually possible to take the address of a nested function, which is done using a technique called trampolines, but beware of the caveats listed in the manual.

like image 133
Adam Rosenfield Avatar answered Sep 28 '22 00:09

Adam Rosenfield