PDA

View Full Version : Nested functions disabled




TotalLuck
Jul 9, 2009, 09:49 PM
I am trying to follow the stanford itunes classes.

As i am watching the lectures, ( which are from this year), they are using examples with nested functions.

However everytime i try to do a simple nested function i get an error that says to use -fnested -function to enable them.
such as this is good

- (IBAction)increaseNumOfSides
{
NSLog(@"im in the increase action");
int n = myPoly.numberOfSides;
++n;
[myPoly setNumberOfSides: n];
[self updateInterface];
}

instead of allowing this

- (IBAction)increaseNumOfSides
{
NSLog(@"im in the increase action");
int n += myPoly.numberOfSides; // this is not allowed
[myPoly setNumberOfSides: n];
[self updateInterface];
}

In this case its such a small amount of code to write i don't really care, however its annoying.

My question is really, why are they disabled in the first place. what problem does nesting your functions cause that Apple recommends that they stay off.

and i have searched the forum and looked to turn them on but i cant seem to find the "other c flags" in xcode to do that. i have gone to the target build menu but i dont see that flag area to put the code in. i am using xcode 3.1.2

any thoughts anyone?

Greg



PhoneyDeveloper
Jul 9, 2009, 11:19 PM
Nested functions are not a part of standard C. They are a GCC language extension. You can find out more about them with Google. I would personally never use them.

I don't think a line like

int n += 5;

is a nested function. I think it's just a syntax error.

TotalLuck
Jul 9, 2009, 11:24 PM
Ok yeah i understand syntax error.

should be

int n;

then i can do
n += myPoly.numberOfSides;



thanks for the reply though

kalimba
Jul 10, 2009, 12:57 AM
Ok yeah i understand syntax error.

should be

int n;

then i can do
n += myPoly.numberOfSides;



thanks for the reply though
You really shouldn't do what those two lines of code do without initializing 'n' to some known value between them. Most compilers will emit a warning that you're using 'n' without first initializing it, or something to that effect.