Prev: function to plot coordinates
Next: Object oriented c++?
From: Keith Thompson on 15 Jul 2010 14:49 neil <invalid(a)invalid.net> writes: > My understanding is that you can call a function within a function but > you cannot define a function within a function, I'm not sure why though. You can't *define* a function within a function, but the OP's code didn't attempt to do that. (The reason, I think, is that references to declarations in the enclosing function can cause some complications; other languages have ways of dealing with those issues, but C and C++ don't). You can either *declare* or call a function within another function. Declaring a function within another function (as the OP inintentionally did) is seldom the right thing to do; function declarations are usually made visable through header files. You can certainly call a function from within another function; the language would be practically useless if you couldn't. -- Keith Thompson (The_Other_Keith) kst-u(a)mib.org <http://www.ghoti.net/~kst> Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"
From: Alf P. Steinbach /Usenet on 15 Jul 2010 15:13 * Keith Thompson, on 15.07.2010 20:49: > neil<invalid(a)invalid.net> writes: >> My understanding is that you can call a function within a function but >> you cannot define a function within a function, I'm not sure why though. > > You can't *define* a function within a function, Well, you can, namely as members of a local class. E.g. void foo() { struct Blah { static void bar() { printf( "Yes, we can!\n" ); } }; Blah::bar(); } > but the OP's code > didn't attempt to do that. (The reason, I think, is that references to > declarations in the enclosing function can cause some complications; > other languages have ways of dealing with those issues, but C and C++ > don't). Yes, those complications are there in full in C++0x, as I understand it. > You can either *declare* or call a function within another > function. Declaring a function within another function (as the > OP inintentionally did) is seldom the right thing to do; function > declarations are usually made visable through header files. I've never yet found a use for a local function declaration. The only thing that this language feature does seems to be to force "the most vexing parse" rule where anything that can be considered a function declaration, is considered a function declaration. Like, if Blah and Gurgle are classes, Blah myVar( Gurgle() ); is not a Blah variable initialized with the default Gurgle value, but is a function taking a Gurgle-something argument and returning Blah. > You can certainly call a function from within another function; the > language would be practically useless if you couldn't. Cheers, - Alf -- blog at <url: http://alfps.wordpress.com>
From: Tim on 15 Jul 2010 16:25 "Keith Thompson" <kst-u(a)mib.org> wrote in message news:lnr5j4a8kh.fsf(a)nuthaus.mib.org... > "Tim" <nospam(a)hotmail.com> writes: > [...] >> time::time(int d, int h, int m, int s) >> >> { >> >> days = d; >> >> hours = h; >> >> minutes = m; >> >> seconds = s; >> >> void adjust_time(); //this doesn't seem to be called >> >> >> } > > That's because it's a declaration, not a function call. Try dropping > the "void" keyword. > > BTW, did you really format your code that way (double-spaced with no > indentation), or did your news software mess it up? Here's how I'd > write it: > > time::time(int d, int h, int m, int s) > { > days = d; > hours = h; > minutes = m; > seconds = s; > adjust_time(); > } > > (Or, depending on my mood and/or local coding standards, I might > put the "{" on the same line as the declaration.) > Thanks for the replies. I feel foolish for not spotting the basic error I made. I copied straight from VC++ into Outlook Express. The code was formated in single spacing with indentation in VC++ but displayed as shown in the newsgroup in OE. I don't know why. Next time I will know :-) --- news://freenews.netfront.net/ - complaints: news(a)netfront.net ---
From: Robert Billing on 15 Jul 2010 17:51 We, the Senate of Arcturus, take note that neil said: > My understanding is that you can call a function within a function but > you cannot define a function within a function, I'm not sure why though. Because, if you remember Algol68, that was one of the things that played merry hell with the stack. Keeping track of where you were involved using things like stack displays. Taking this out was one of the moves that enabled Richards to make BCPL reasonably small, and the same idea was propagated into C. After all, C is merely the somewhat wayward child of BCPL.
From: Keith Thompson on 15 Jul 2010 17:55 "Alf P. Steinbach /Usenet" <alf.p.steinbach+usenet(a)gmail.com> writes: > * Keith Thompson, on 15.07.2010 20:49: [...] >> You can either *declare* or call a function within another >> function. Declaring a function within another function (as the >> OP inintentionally did) is seldom the right thing to do; function >> declarations are usually made visable through header files. > > I've never yet found a use for a local function declaration. Well, I suppose it could be used to enforce and/or document the fact that a given function is only meant to be called from one place. void foo() { void foo_helper(); // ... foo_helper(); } void bar() { // foo_helper is not visible here } But it's not something I'd do, and there are probably better ways to structure your code to express the same thing. -- Keith Thompson (The_Other_Keith) kst-u(a)mib.org <http://www.ghoti.net/~kst> Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: function to plot coordinates Next: Object oriented c++? |