GNU C/C++/Objective-C 4.2.1 Compiler User Guide

5.4 Nested Functions

A nested function is a function defined inside another function.
Nested functions are not supported for GNU C++ and are disable by
default on Darwin. The -fnested-functions and
-fno-nested-functions options can be used to enable and
disable nested function suppport. The nested function's name is local
to the block where it is defined. For example, here we define a
nested function named square, and call it twice:

The nested function can access all the variables of the containing
function that are visible at the point of its definition. This is
called lexical scoping. For example, here we show a nested
function which uses an inherited variable named offset:

Here, the function intermediate receives the address of
store as an argument. If intermediate calls store,
the arguments given to store are used to store into array.
But this technique works only so long as the containing function
(hack, in this example) does not exit.

If you try to call the nested function through its address after the
containing function has exited, all hell will break loose. If you try
to call it after a containing scope level has exited, and if it refers
to some of the variables that are no longer in scope, you may be lucky,
but it's not wise to take the risk. If, however, the nested function
does not refer to anything that has gone out of scope, you should be
safe.

GCC implements taking the address of a nested function using a technique
called trampolines. A paper describing them is available as

A nested function can jump to a label inherited from a containing
function, provided the label was explicitly declared in the containing
function (see Local Labels). Such a jump returns instantly to the
containing function, exiting the nested function which did the
goto and any intermediate functions as well. Here is an example:

A nested function always has no linkage. Declaring one with
extern or static is erroneous. If you need to declare the nested function
before its definition, use auto (which is otherwise meaningless
for function declarations).