My code looks like
- int add_two_numbers:(int)number1 secondnumber:(int)number2;
int main()
{
return 0;
}
- int add_two_numbers:(int)number1 secondnumber:(int)number2
{
return number1 + number2;
}
I got error saying "missing context for method declaration" and "expected method body". I am following the tutorials on tutorialpoints on objective-c, but it is very vague in this section. It seems like the methods have to be in some classes, and cannot go alone like what I did. What's going on?
Methods like the second one can only live in classes. You can use the C stand-alone syntax whenever you want a stand-alone function.
Also, your syntax is slightly off -- in a class, you'd declare it like this:
- (int)add_two_numbers:(int)number1 secondnumber:(int)number2
{
return number1 + number2;
}
With the return type in parentheses.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With