return 0
return 0

Reputation: 4366

How to declare and define functions in objective-c like in c?

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?

Upvotes: 5

Views: 6284

Answers (1)

Lou Franco
Lou Franco

Reputation: 89152

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.

Upvotes: 5

Related Questions