37

What is the difference between the two? If I'm writing a program, when would I need a this:

void aFunction() {
    //do something
}

and when would I need this:

-(void)aMethod {
    //do something else
}
Jumhyn
  • 6,687
  • 9
  • 48
  • 76

3 Answers3

80

Actually, an Objective-C method is just a C function with two arguments always present at the beginning.

This:

-(void)aMethod;

Is exactly equivalent to this:

void function(id self, SEL _cmd);

Objective-C's messaging is such that this:

[someObject aMethod];

Is exactly equivalent to this (almost -- there is a variadic argument ABI issue beyond the scope of this answer):

objc_msgSend(someObject, @selector(aMethod));

objc_msgSend() finds the appropriate implementation of the method (by looking it up on someObject) and then, through the magic of a tail call optimization, jumps to the implementation of the method which, for all intents and purposes, works exactly like a C function call that looks like this:

function(someObject, @selector(aMethod));

Quite literally, Objective-C was originally implemented as nothing but a C preprocessor. Anything you can do in Objective-C could be rewritten as straight C.

Doing so, however, would be a complete pain in the ass and not worth your time beyond the incredibly educational experience of doing so.


In general, you use Objective-C methods when talking to objects and function when working with straight C goop. Given that pretty much all of Mac OS X and iOS provide Objective-C APIs -- certainly entirely so for the UI level programming entry points -- then you use Obj-C most of the time.

Even when writing your own model level code that is relatively standalone, you'll typically use Objective-C simply because it provides a very natural glue between state/data & functionality, a fundamental tenant of object oriented programming.

bbum
  • 162,346
  • 23
  • 271
  • 359
9

In Objective-C each function operates on an object, like

[myObject myFunction]

A C method has the form:

return-type function-name(argument1, argument2, etc) {}

An Objective-C instance method has the form:

-(return-type)function-name:argument1 {}

or for a multi-argument function

-(return-type)function-name:argument1 function-name:argument2 {}

I always use Objective-C-style methods in Obj-C programming, even though you can still use C-type functions as well.

I suppose the equivalent in C to [myObject myMethod:arg] might be myObject.myMethod(arg)

aqua
  • 3,269
  • 28
  • 41
  • 7
    Actually in C there are no methods, just freestanding functions; the last "equivalent C" syntax you wrote is the *C++* syntax for instance methods. – Matteo Italia Jan 31 '11 at 01:25
  • To me, methods and functions are the same things. i.e. interchangeable in language. Some programming languages do make distinctions, but across the board you'll have some people using function where method is appropriate and vice versa. – aqua Jan 31 '11 at 01:27
  • 5
    @aqua: AFAIK, you talk about methods when you're talking about objects (http://en.wikipedia.org/wiki/Method_%28computer_programming%29), otherwise you have a function or a subroutine. Still, my fundamental point was that the last "equivalent C" syntax you wrote doesn't make sense, because in C there are no objects (`struct`s don't have instance methods). Instead, that is the syntax used in **C++** for instance methods. – Matteo Italia Jan 31 '11 at 01:32
  • @Matteo Italia: But `myObject` could be an instance of a `struct` with a member called `myMethod` that is a pointer to a function, then `myObject.myMethod(arg)` would be perfectly valid **C**. Such constructs are quite common when implementing objects in C. – mu is too short Jan 31 '11 at 01:42
  • @mu: I may still say that yours is a *usage* point of view while mine is a *language* POV. But I'll just say "lesson learned". `:)` – Matteo Italia Jan 31 '11 at 10:08
  • @aqua - is there any reason why I shouldn't use C-style arguments? I am working on a simple project where I use obj-c style for the UI stuff, but when I am in a button, it runs a few functions that gathers the data to run calculations. Since perhaps I am still used to doing things in C++ it was just easier for me to have an int foo(int, int, int) and int bar(foo), etc., so for such an elementary function, while it does compile and I do get the correct results, is there a reason I should get into the habit of changing the way I code such trivial functions? – Jonathan Weinraub Apr 22 '13 at 16:21
  • @JonathanWeinraub - yes, if anyone else will be reading your code. If this is strictly for personal development then code however you feel most comfortable. – aqua Apr 22 '13 at 16:29
  • @aqua Thanks for the fast reply! Well this will be for my own personal development as I learn. But if in the future I start doing more public coding I rather not get into the habit of writing bad code or getting into bad practices. I rather do things correct now rather than trying to break bad habits later on... Like for now I really hate how the curly braces are on the same line since I started to code in C++ with braces on separate lines ... – Jonathan Weinraub Apr 22 '13 at 16:48
2

The first is a freestanding function. The second is an instance method for an Objective-C class. So I guess you would need the second version if you're actually writing a class.

Etienne de Martel
  • 34,692
  • 8
  • 91
  • 111