declaring a function in a function - C programming
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
declaring a function in a function - C programming
hi I want to declare a function in a function, but had no success till now, see the error code below and visit the project at sourceforge
./audio/ags_channel.o: In function `ags_channel_recursive_play_init_down_input':
/home/joel/ags/./audio/ags_channel.c:751: undefined reference to `ags_channel_recursive_play_init_down'
/home/joel/ags/./audio/ags_channel.c:769: undefined reference to `ags_channel_recursive_play_init_down'
/home/joel/ags/./audio/ags_channel.c:772: undefined reference to `ags_channel_recursive_play_init_down'
collect2: ld gab 1 als Ende-Status zurück
make: *** [ags] Fehler 1
Nested functions are not allowed in ISO C - I believe gcc will do them for you, but they're best avoided for portability. Just declare your function static and be done with it.
Nested functions are not allowed in ISO C - I believe gcc will do them for you, but they're best avoided for portability. Just declare your function static and be done with it.
AFAIR, LLVM now officially supports closures, so declaration of a function inside another function is possible. And full closures support is much better than what 'gcc' has.
LLVM also allows to translate C++ (and "C" for that matter) into very primitive (in a good sense) "C", so this, I think, paves th road to portability. I.e. that primitive "C" is portable.
AFAIR, LLVM now officially supports closures, so declaration of a function inside another function is possible. And full closures support is much better than what 'gcc' has.
LLVM also allows to translate C++ (and "C" for that matter) into very primitive (in a good sense) "C", so this, I think, paves th road to portability. I.e. that primitive "C" is portable.
What on earth do LLVM or closures have to do with this? Whether the variables in a function capture the lexical environment or not is not what this post is about.
This post is about defining functions within functions in C. That breaks portability with other compilers because it's non-standard.
To the OP: If you're desperate to use such a feature, use another language - otherwise, just declare and define all functions at the top-level and you'll save yourself trouble in the long run.
Nested functions are not allowed in ISO C - I believe gcc will do them for you, but they're best avoided for portability. Just declare your function static and be done with it.
This doesn't really answer the question of the OP, but just a kind of completion. gcc allows nested functions as part of its GNU extensions (enabled by default). Though, if you don't specify any explicit standard (e.g. --std=c99), you'll get support for nested functions, but just like JohnGraham said, this is non-standard. If you'll ever compile this code with gcc, you'll be safe :-) But, you shouldn't possibly trust on that ... To be at least aware of this non-standard feature, I would recommend to use the -pedantic option which warns about non-standard usage tolerated by GNU extensions!
What on earth do LLVM or closures have to do with this? Whether the variables in a function capture the lexical environment or not is not what this post is about.
...
Now, tell what for would you declare a function inside another function if you do not want to capture variables in the enclosing scope ?
Now, tell what for would you declare a function inside another function if you do not want to capture variables in the enclosing scope ?
You seem determined to pull this thread off-topic. Whatever the relative merits of declaring functions inside functions and whether they form lexical closures or not, the OP's question seems to have been answered.
You seem determined to pull this thread off-topic. Whatever the relative merits of declaring functions inside functions and whether they form lexical closures or not, the OP's question seems to have been answered.
I did read about functions declared inside functions in 'gcc', and even tried this feature. I'm just reiterating what I've said: if somebody needs a function declared inside another function, most likely he/she actually needs full closures support.
AFAIR, LLVM also supports ref-counted variables needed for full closures support.
Questionable. Closures are part of the upcoming C++ standard.
The existence of closures in C++0x doesn't say *anything* about whether or not they belong in C (they don't). The "language" of "C/C++" is one of the greatest misconceptions of C++. They are very different languages with very different domains.
The existence of closures in C++0x doesn't say *anything* about whether or not they belong in C (they don't). The "language" of "C/C++" is one of the greatest misconceptions of C++. They are very different languages with very different domains.
Closures in "C" make it much more potent and closer to C++ from the point of view of functionality.
As far as I know, closures are being considered for part of the new C++ standard.
Either way, that's even more reason to avoid using them - then, you would be able to compile the same code with (1) a C compiler that treats the function definition as a normal function definition that captures nothing about the lexical environment and (2) a C++ compiler that captures the lexical environment.
Not only will they both compile as if nothing were wrong, but a C or C++ programmer (especially one that's not been exposed to functions as first-class objects, lexical closures and such) will have a very, very hard time tracking down the bugs that introduce, which will probably be many, colourful and varied.
Quote:
Originally Posted by Sergei Steshenko
Quote:
Originally Posted by smeezekitty
I don't know what closures are, but putting a function in a function is a bad idea.
Exactly because you do not know what closures are you think it's a bad idea.
I'm sorry, but I feel compelled to point out that this post makes you come across as both pompous and dismissive.
I know what closures are and even if C had closures, I'd think putting a function in a function is still a bad idea (indeed, a much worse idea than I already do), partly because it's so utterly against the philosophy of the rest of the language.
Last edited by JohnGraham; 05-26-2010 at 06:28 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.