Home > DeveloperSection > Forums > Use of undeclared identifier “sum”
Glen Martin
Glen Martin

Total Post:27

Points:191
Posted on    August-04-2015 11:23 PM

 iPhone iPhone  Ios  Objective C 
Ratings:


 1 Reply(s)
 557  View(s)
Rate this:

I have repeated error 3rd time, but I could not found that what is mean of use of undeclared identifier ..

I wrote that program on function in objective-C

#import <Foundation/Foundation.h>

 

@interface Add:NSObject

 

/* method declaration */  - (int)add:(int)a andNum2:(int)b;

@end

@implementation Add

 

/* method returning the max between two numbers */  - (int)add:(int)a andNum2:(int)b {

/* local variable declaration */

int sum = a +b;

return sum;

}

@end

NSLog(@"sum is : %d", sum);// error this line    

  return 0;

}



Tarun Kumar

Total Post:213

Points:1635
Posted on    August-05-2015 11:48 PM

You can do like here:

#import <Foundation/Foundation.h>

 

@interface Add:NSObject {

  int sum;//Declare sum as global variable to access in class

 }

- (int)add:(int)a andNum2:(int)b;

 @end

 

 @implementation Add

   - (int)add:(int)a andNum2:(int)b {

   sum = a +b;

   return sum;

}

 

@end

NSLog(@"sum is : %d", sum);//Now Access global variable with in class.


Don't want to miss updates? Please click the below button!

Follow MindStick