5

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?

4
  • 4
    That tutorial is doing an absolutely terrible job of teaching you about this. Functions exist in ObjC, distinctly from methods; they are related at a deeper level, but not the same thing. "Basically in Objective-C, we call the function as method" is nonsense. I would suggest finding another resource to learn from. Have a look at Good resources for learning ObjC Commented Jan 29, 2016 at 19:30
  • 1
    you can directly call c's function from objective c. Commented Jan 29, 2016 at 19:36
  • 2
    You could make that a normal C function: int add_two_numbers(int number1, int number2) Commented Jan 29, 2016 at 20:01
  • 1
    That tutorial is just flat out wrong in a number of places. I would recommend against using it for anything. Commented Jan 30, 2016 at 6:00

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Except you shouldn't use '_' and it should be camel cased. I.e. - (NSInteger) addNumberOne:(NSInteger)numberOne toSecondNumber:(NSInteger) numberTwo; or the like.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.