Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
After I read the Learn C on Mac book I took the Pascal class at my local City College. One thing I learned there was to keep my 'Main' clean looking and send out Procedures and Functions for processing and return the data. This made the code easier to read.

One thing jiminaus mentioned in another thread I had was that NSLog was a Function and not a Method. So that I have my terminology right. A Method is just simply procedural code, that performs a task and returns the result. I can pass a parameter from Main, to a Method in an object, and return a result back to Main again(keeping it simple). That Method could be anything from just storing a simple Integer to a Function or some very complex procedure.

-Lars
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
A method is a "function" within the scope of an object. A function is simply a procedural call. So it's basically a difference of programming paradigm.
 

balamw

Moderator emeritus
Aug 16, 2005
19,366
979
New England
A Method is just simply procedural code, that performs a task and returns the result.

That sounds more like the difference between a subroutine (void function in C) and function.

Methods are functions that operate on objects.

B
 

chown33

Moderator
Staff member
Aug 9, 2009
10,751
8,423
A sea of green
Methods are functions that operate on objects.

Except you can define functions that operate on objects, too. For example:
Code:
void funky( int foo, NSMutableString * bar, float bazz );
Or consider NSLog(), which is a function that operates on objects.


I often find it useful to look up terms using Wikipedia. Even if I disagree with their definition, I frequently find the discussion and rationale useful.
http://en.wikipedia.org/wiki/Method_(computer_programming)
In object-oriented programming, a method is a subroutine that is exclusively associated either with a class (in which case it is called a class method or a static method) or with an object (in which case it is an instance method).​

For example, every class descended from NSObject has a -description method. The effect is that every object of every descendant class can respond to the -description message. Classes can override the inherited method, causing only instances of that class to always perform the new method.
 

balamw

Moderator emeritus
Aug 16, 2005
19,366
979
New England
It's the "exclusivity" that's harder to pin down.

e.g. an accessor method is a function that generally takes an object and has a return value associated with the state of the object.

However, it typically doesn't modify the object, and its return value is generally not an object...

B
 

Sydde

macrumors 68030
Aug 17, 2009
2,552
7,050
IOKWARDI
I look at it this way: an object instance is the context in which a method runs. The method can see the instance variables defined in the @interface section. A function does not have any private persistent context (unless you define a static within its scope), only whatever you give it when you call it.

When you send a message (call a method), the underlying Objective-C processes have to find the method (usually very quickly), load the context (the pointer to the receiving object) and run the code. A function begins much more quickly because the location of its code is already know and there is no context to load. With Objective-C, you sacrifice a small amount of run time to gain a lot of flexibility.
 

GorillaPaws

macrumors 6502a
Oct 26, 2003
932
8
Richmond, VA
If you think of objects as actors in a play you're writing, then methods are messages between the objects (dialogue between actors), or messages to themselves (monologues). Class methods in this analogy take on a meta role and might be something like, how many actors do we have to play the Hamlet role in this year's broadway production? let's fire John Doe from the Hamlet role and cast Will Smith for the part instead, etc.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.