Hello all!
I just started learning Objective-C and OOP ... so I have very stupid question :) I wanna understand how to write recursive functions in Objective-C.
I get factorial counting as example. Here is my code )) he-he
#import <Foundation/Foundation.h>
@interface Factorial : NSObject
{
int nFact;
}
-(int) countFactorial:(int) nFact;
-(void) printFactorial;
@end //Factorial
@implementation Factorial
-(int) countFactorial:(int) n
{
int tmp;
if (n!=0)
{
tmp=n*countFactorial(n-1);
}
else {
return(0);
}
nFact=tmp;
}
-(void) printFactorial
{
NSLog(@"Factorial = %i",nFact);
}
@end //Factorial
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
id myFact=[Factorial new];
int qqq=[myFact countFactorial:5];
[myFact printFactorial];
[myFact release];
[pool drain];
return 0;
}
I get error from compiler - that I implicit declare countFactorial function and I get error (of course) from linker that it didn't find countFactorial
Plz give my any suggestions how implement recursion in Objective-C I want to use it im my next "project" - tic tac toe - to find right move via recursion )
Thx!