ネストされた関数が C で許可されていることをどこかで読みました (少なくとも GNU コンパイラでは許可されています)。次のコードを検討してください。
/* nestedfunc.c */
#include <stdlib.h> /* for atoi(3) */
#include <stdio.h>
int F (int q)
{
int G (int r)
{
return (q + r);
}
return (G (5));
}
int main (int argc, const char* argv[])
{
int q = 0;
if (argc > 1)
{
q = atoi (argv[1]);
}
printf ("%d\n", F (q));
return 0;
}
コンパイルと実行:
gcc -o nestedfunc -O2 -s -Wall nestedfunc.c
me@mybox:~/college/c++/other stuff$ ./nestedfunc 8
13
me@mybox:~/college/c++/other stuff$
また、他のいくつかのプログラミング言語がこれらをサポートしていることも読みました。私の質問はこれです: ネストされた関数にはどのような有用な目的がありますか? 前もって感謝します。