Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

objective-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?

like image 899
return 0 Avatar asked Jan 29 '16 19:01

return 0


1 Answers

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.

like image 69
Lou Franco Avatar answered Oct 08 '22 04:10

Lou Franco