Nested Function in C

Can we have a nested function in C? What are the benefits of nested functions? If they exist in C, is their implementation different from compiler to compiler?

+83
c function nested
Apr 09 '10 at 14:13
source share
9 answers

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

You can declare a function inside a function, but it is not a nested function.

gcc has a language extension that allows nested functions . They are non-standard and, as such, are completely dependent on the compiler.

+95
Apr 09 '10 at 14:14
source share

No, they do not exist in C.

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

  • They allow functional decomposition without polluting namespaces. You can define one public function that implements some complex logic, relying on one or more nested functions to break the problem down into smaller logical fragments.
  • They simplify the transfer of parameters in some cases. A nested function has access to all parameters and some or all variables in the field of an external function, so an external function does not need to explicitly pass a bunch of local state to the nested function.
+33
Apr 09 '10 at 14:14
source share

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

+18
Apr 09 '10 at
source share

No, you cannot have a nested function in C Closest you can specify a function inside the definition of another function. However, the definition of this function must appear outside of any other function body.

eg.

 void f(void) { // Declare a function called g void g(void); // Call g g(); } // Definition of g void g(void) { } 
+15
Apr 09 '10 at 14:15
source share

I mention this since many people encoding C now use C ++ compilers (such as Visual C ++ and Keil uVision) so you can use this ...

Although C is not yet allowed if you use C ++, you can achieve the same effect using the lambda functions introduced in C ++ 11:

 void f() { auto g = [] () { /* Some functionality */ } g(); } 
+5
May 2 '14 at 13:49
source share

Like others, the C standard does not support nested functions.

Nested functions are used in some languages ​​to include several functions and variables in a container (external function), so that individual functions (excluding an external function) and variables are not visible from the outside.

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

+2
Apr 09 2018-10-09T00:
source share

To answer your second question, there are languages ​​that allow you to define nested functions (the list can be found here: nested functions-language-list-wikipedia ).

In JavaScript, which is one of the most famous of these languages, some uses of nested functions (called closures):

  • Creating class methods in object constructors.
  • To achieve the functionality of private class members along with setters and getters.
  • Do not pollute the global namespace (of course, for each language).

to name a few ...

+1
Apr 24 '17 at 18:52
source share

Or you can be smart about this and use the preprocessor to your advantage ( source.c ):

 #ifndef FIRSTPASS #include <stdio.h> //here comes your "nested" definitions #define FIRSTPASS #include "source.c" #undef FIRSTPASS main(){ #else int global = 2; int func() {printf("%d\n", global);} #endif #ifndef FIRSTPASS func();} #endif 
0
Aug 08 '19 at 17:16
source share

Is this not a nested function in C? (function displayAccounts ())

I know that I could define a function in different ways and pass variables and what not, but it still works well, since I needed to print accounts several times.

(snipet taken from school assignment) ...

 //function 'main' that executes the program. int main(void) { int customerArray[3][3] = {{1, 1000, 600}, {2, 5000, 2500}, {3, 10000, 2000}}; //multidimensional customer data array. int x, y; //counters for the multidimensional customer array. char inquiry; //variable used to store input from user ('y' or 'n' response on whether or not a recession is present). //function 'displayAccounts' displays the current status of accounts when called. void displayAccounts(void) { puts("\t\tBank Of Despair\n\nCustomer List:\n--------------"); puts("Account # Credit Limit\t Balance\n--------- ------------\t -------"); for(x = 0; x <= 2; x++) { for(y = 0; y <= 2; y++) printf("%9d\t", customerArray[x][y]); puts("\n"); } } displayAccounts(); //prints accounts to console. printf("Is there currently a recession (y or n)? "); //... return 0; } 
-one
Apr 01 '15 at 12:22
source share



All Articles