Things in Objects.

Discussion in 'Mac Programming' started by larswik, Jun 17, 2011.

  1. macrumors 65816

    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.

  2. Moderator


    Staff Member

    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.
  3. macrumors 65816

    That I grasp now, Methods are just Functions. Thanks.
  4. Moderator


    Staff Member

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

    Methods are functions that operate on objects.

  5. macrumors 603

    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.
  6. Moderator


    Staff Member

    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...

  7. macrumors 68000


    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.
  8. macrumors 6502a


    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.
  9. macrumors 65816

    Thanks for you help

Share This Page