Hi, how to pass multiple arguments in a single function in Objective-C? I want to pass 2 integer values and the return value is also integer. I want to use the new Objective-C syntax, not the old C/C++ syntax.
A:
Like this:
int sum(int a, int b) {
return a + b;
}
Called like this:
int result;
result = sum(3, 5);
// result is now 8
T.J. Crowder
2010-04-01 12:12:59
sorry boss, i need the code in objective c not in simple c or c++
iPhone Fun
2010-04-01 12:22:35
@Viral: That *is* Objective-C. See the link.
T.J. Crowder
2010-04-01 12:23:38
@Viral: I've updated your question to make it clearer what you were looking for. Remember that the clearer you make your question, the higher the quality of answers you'll get.
T.J. Crowder
2010-04-01 12:36:33
thanks for ur suggestion , i'll be clear since ....
iPhone Fun
2010-04-01 12:45:18
You could at least tell the OP where you copied and pasted that from (http://en.wikibooks.org/wiki/Objective-C_Programming/syntax).
T.J. Crowder
2010-04-01 12:22:56
sorry dear, i need the answer in objective c language, not in c or c++.
iPhone Fun
2010-04-01 12:23:19
+2
A:
In objective-c it is really super easy. Here is the way you would do it in C:
int functName(int arg1, int arg2)
{
// Do something crazy!
return someInt;
}
This still works in objective-c because of it's compatibility with C, but the objective-c way to do it is:
// Somewhere in your method declarations:
- (int)methodName:(int)arg1 withArg2:(int)arg2
{
// Do something crazy!
return someInt;
}
// To pass those arguments to the method in your program somewhere:
[objectWithOurMethod methodName:int1 withArg2:int2];
Best of luck!
James
2010-04-01 12:23:29