View Full Version : Things in Objects.

Jun 17, 2011, 04:54 PM
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.


Jun 17, 2011, 05:41 PM
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.

Jun 17, 2011, 06:10 PM
That I grasp now, Methods are just Functions. Thanks.

Jun 17, 2011, 06:14 PM
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.


Jun 17, 2011, 07:41 PM
Methods are functions that operate on objects.

Except you can define functions that operate on objects, too. For example:
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.
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.

Jun 17, 2011, 08:16 PM
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...


Jun 19, 2011, 10:06 AM
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.

Jun 19, 2011, 11:44 PM
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.

Jun 20, 2011, 02:04 AM
Thanks for you help