-1

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;
}
soumya
  • 3,801
  • 9
  • 35
  • 69
venky ios
  • 1
  • 2
  • 3
    `sum` is a local variable - you can't access it from outside the function. This is very basic stuff, so I suggest you might want to get a good book on C/Objective C. – Paul R Aug 05 '15 at 05:44

2 Answers2

0
#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     
soumya
  • 3,801
  • 9
  • 35
  • 69
0

Is this the exact code? if yes then first you need to call

NSLog(@"sum is : %d", sum);
return 0;

inside some function.

Also sum is a variable declared inside add method and thus cannot be used outside that function. Declare sum under @interface Add:NSOBject to use it out side add method.

If you don't want to declare it outside add method then change the line

NSLog(@"sum is : %d", sum);

to

NSLog(@"sum is : %d", [add:@(12) andNum2:@(13)]);

(Note: Replace 12 and 13 with any other number or numeric variable)

Anas iqbal
  • 1,036
  • 8
  • 23
  • /* local variable definition */ int a = 10; int b = 20; when i was replacing shown error i.e unused variable a, b NSLog(@"sum is : %d", [Add:@(10) andNumb2:@(20)]); retur – venky ios Aug 05 '15 at 06:01
  • replace `@(10)` and `@(20)` with `a` and `b` respectively. i.e. `NSLog(@"sum is : %d", [Add:a andNumb2:b]);` – Anas iqbal Aug 06 '15 at 05:08