tags:

views:

239

answers:

5

Can we have a nested function in C? What is the use of nested functions? If they exist in C does there implementation differes from compiler to compiler.

Are nested functions allowed in any other language? If yes then what is there significance?

+3  A: 

No, they don't exist in C.

They are used in languages like Pascal for (at least) two reasons:

  1. They allow functional decomposition without polluting namespaces. You can define a single publically visible function that implements some complex logic by relying one or more nested functions to break up the problem into smaller logical pieces.
  2. They simplify parameter passing in some cases. A nested function has access to all the parameters and some or all of the variables in the scope of the outer function, so the outer function doesn't have to explicitly pass a pile of local state into the nested function.
Marcelo Cantos
+8  A: 

You cannot define a function within another function in standard C.

You can declare a function inside of a function, but it's not a nested function.

gcc has a language extension that allows nested functions. They are nonstandard, and as such are entirely compiler-dependent.

James McNellis
+3  A: 

No you can't have a nested function in C. The closest you can come is to declare a function inside the definition of another function. The definition of that function has to appear outside of any other function body, though.

E.g.

void f(void)
{
    // Declare a function called g
    void g(void);

    // Call g
    g();
}

// Definition of g
void g(void)
{
}
Charles Bailey
If the function g is declared in this way, then what will be its scope?
Sachin Chourasiya
The declaration is scoped like any other declaration, so in this case until of the end of the function. Of course once the definition of `g` is visible later in the file that declaration is in scope for the rest of the translation unit. In addition you _can_ call functions in C without a visible declaration in scope even if it isn't advisable.
Charles Bailey
+1  A: 

Nested functions are not a part of ANSI C, however, they are part of Gnu C.

zoli2k
What is there significance if they are part of Gnu C
Sachin Chourasiya
@Sachin Helps to realize why the C code with nested functions can be compiled with gcc. The information has educational value. Moreover, the question doesn't specified if it is limited only to C89, C99 or GNU C
zoli2k
Other languages supported by GCC do have them (ADA and Pascal that I know of), so it is likely that either it was easy to add to the C implementation or that it was added to C in order to make in preparation for supporting languages which require them.
nategoose
@nategoose Good point, 1+.
zoli2k
A: 

As others have answered, standard C does not support nested functions.

Nested functions are used in some languages to enclose multiple functions and variables into a container (the outer function) so that the individual functions (excluding the outer function) and variables are not seen from outside.

In C, this can be done by putting such functions in a separate source file. Define the main function as global and all the other functions and variables as static. Now only the main function is visible outside this module.

PauliL