LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-24-2010, 02:01 PM   #1
joel2001k
Member
 
Registered: Mar 2007
Distribution: GNU/Linux debian unstable main
Posts: 95

Rep: Reputation: 17
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
 
Old 05-24-2010, 02:34 PM   #2
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
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.
 
Old 05-24-2010, 09:42 PM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by JohnGraham View Post
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.
 
Old 05-25-2010, 04:00 AM   #4
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by Sergei Steshenko View Post
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.
 
Old 05-25-2010, 05:54 AM   #5
ForzaItalia2006
Member
 
Registered: Dec 2009
Location: Walldorf, Germany
Distribution: (X)Ubuntu, Arch, Gentoo
Posts: 205

Rep: Reputation: 67
Quote:
Originally Posted by JohnGraham View Post
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!
 
Old 05-25-2010, 06:46 AM   #6
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by JohnGraham View Post
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 ?
 
Old 05-25-2010, 07:32 AM   #7
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by Sergei Steshenko View Post
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.
 
Old 05-25-2010, 12:12 PM   #8
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by JohnGraham View Post
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.
 
Old 05-25-2010, 12:30 PM   #9
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
I don't know what closures are, but putting a function in a function is a bad idea.
 
Old 05-25-2010, 12:43 PM   #10
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by smeezekitty View Post
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.

Last edited by Sergei Steshenko; 05-26-2010 at 12:04 AM.
 
Old 05-25-2010, 02:53 PM   #11
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Closures are indeed awesome, but if you really want/need to use them C is almost certainly the wrong language for the job.
 
Old 05-26-2010, 12:05 AM   #12
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by tuxdev View Post
Closures are indeed awesome, but if you really want/need to use them C is almost certainly the wrong language for the job.
Questionable. Closures are part of the upcoming C++ standard.
 
Old 05-26-2010, 02:43 AM   #13
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Quote:
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.
 
Old 05-26-2010, 04:00 AM   #14
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by tuxdev View Post
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.
 
Old 05-26-2010, 06:26 AM   #15
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by Sergei Steshenko View Post
Closures are part of the upcoming C++ standard.
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing data from interrupt handler function to tasklet function in kernel programmin double-out Programming 2 05-18-2010 10:10 PM
[SOLVED] Threaded function cannot call a function with extern "C" but nonthreaded function can morty346 Programming 16 01-12-2010 05:00 PM
how to print function names & parmaters each time control enters the function? tanniru Linux - Networking 1 09-11-2008 01:21 AM
Compilation issue when Function is parameter in function call on LINUX sa20358 Linux - Software 2 07-24-2008 10:19 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:08 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration