Objective-C: use a pointer, or use return?

Discussion in 'Mac Programming' started by Sandy Santra, Apr 8, 2009.

  1. macrumors 6502

    Joined:
    Feb 1, 2008
    Location:
    Brooklyn
    #1
    I'm a slightly confused newbie learning basic Objective-C from BecomeAnXcoder, and I'm wondering: when do you use "return" to return the result of a function call, and when (instead) do you use pointers? These are two different ways of handling scope limitations, as far as I can see, but I don't understand when/why to use each.

    Thanks in advance for your thoughts, help, and explanations.
     
  2. macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #2
    You would use the return statement in both cases, except you would just return a pointer rather than say an int.

    Unless I am misunderstanding the question. All a pointer does is point to an area of memory which contains some data.
     
  3. macrumors 6502a

    GorillaPaws

    Joined:
    Oct 26, 2003
    Location:
    Richmond, VA
    #3
    Returns and pointers are not mutually exclusive.
     
  4. thread starter macrumors 6502

    Joined:
    Feb 1, 2008
    Location:
    Brooklyn
    #4
    Hmmm...

    Well, the book first teaches about using return statements to pass the value back from the call after the function is executed.

    Then 10 pages later it shows almost identical code for the function but without the return statement. It immediately thereafter says: "See! The function doesn't work! That's why you need to be using pointers! Now we'll talk about pointers, even though it's so advanced you may not get it."

    And I don't. But it DOES totally make sense that they're not mutually exclusive.

    What DOESN'T make sense is that they introduce the idea of pointers as if to advise: "The return statement is for children; what you really need are pointers!" (tm)
     
  5. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    You can only return one thing. It can be a structure, an object with lots of fields, an array of objects, etc. so there can be a lot of "stuff"... but it's still just one thing. If you need to modify many things, you can pass pointers to the method and have it modify them.

    Otherwise... do you need to get something "new" from the method, or do you need to modify something that already exists? If the former, return, if the latter, pass the pointer to the thing.

    -Lee
     
  6. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #6
    They are trying to be funny/facetious. Returns always have a place. The biggest thing you could return is a complex structure, but normally you're returning a pointer or other primitive, in which case... those 8 bytes aren't hurting you that much. If you're at the point that you're building giant structures, you should know by then that passing a pointer around is a better idea.

    -Lee
     
  7. thread starter macrumors 6502

    Joined:
    Feb 1, 2008
    Location:
    Brooklyn
    #7
    Actually, they don't:

    void increment(int *y)
    {
    *y = *y + 1;
    }

    ...

    int myValue = 6;
    increment(&myValue);
     
  8. thread starter macrumors 6502

    Joined:
    Feb 1, 2008
    Location:
    Brooklyn
    #8
    My impression is that the code can "return" something even when the "return" statement isn't used in the function, i.e.:

    void increment(int *y)
    {
    *y = *y + 1;
    }

    If you just modify the value of a variable, it doesn't "stick" (globally), right? Whereas, if you use a pointer to modify the value, it seems that it does stick.

    The book says:

    "If you want to write a version of the increment function that actually works, i.e. accepts a variable as its argument and permanently increases the value of that variable, you need to pass it the address of a variable."

    So they make it sound like you HAVE to use addresses to get anything done.

    Whereas, I thought you could use a return statement (instead).
     
  9. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #9
    In this case it can be done both ways, but often you write functions that accept a pointer to a struct and then the function modifies that struct.

    So here is the same thing but using a return statement:

    Code:
    int increment(int y)
    {
        return y + 1;
    }
    
    ...
    
    int myValue = 6;
    myValue = increment(myValue);
    By not using a pointer the value of myValue is copied, but you could change it to use the pointer instead.
     
  10. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #10
    Code:
    int increment(int y) {
      return y+1;
    }
    int main(int argc, char *argv[]) {
      int z = 1;
      z=increment(z);
    }
    
    There is always a way. The text your reading is just speaking more colloquially.

    -Lee

    EDIT: Kainjow beat me
     
  11. thread starter macrumors 6502

    Joined:
    Feb 1, 2008
    Location:
    Brooklyn
    #11
    OK, that is totally making sense.

    Advantages/disadvantages of either method?

    For instance is one method better for returning large structures? Or is one of these methods better for small, simple functions (or methods)?
     
  12. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #12
    Better is relative. Is it easier for you to understand when something does a return? Then maybe that's better.

    In absolute terms, it's cheaper to push a pointer onto the stack instead of a structure that's larger than the size of pointer on there. However, if you're writing objective-C, most things will be objects, not structures, so you'll always be passing a pointer no matter what.

    For primitives, a pointer is about the same size (within a few bytes), so it's more a matter of style and need.

    -Lee
     
  13. macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #13
    One style that is enabled by using 'return' is "chaining" of function calls or method calls together.

    So for example:

    Code:
    [[[NSObject alloc] init] autorelease]
    
    would not be possible if 'alloc' and 'init' didn't return the id of the object. BTW, 'id' is a pointer.
     
  14. thread starter macrumors 6502

    Joined:
    Feb 1, 2008
    Location:
    Brooklyn
    #14
    Thanks, all, for everyone's great answers. Huge help.

    Still getting my feet wet, so some of this is over my head, but it's all helpful, and also very interesting and exciting.
     

Share This Page