Method return values

Discussion in 'iOS Programming' started by childoftheko4n, Jul 10, 2013.

  1. childoftheko4n macrumors regular

    Joined:
    Oct 18, 2011
    #1
    Hey all, been reading Programing in Objective-C 4th edition and have a really basic question. I think i sort of understand why but am not 100% clear.

    when declaring and defining methods, when to use void and when not (and to return a value). The grey area in my my is there because of something like this sample code:

    implementation:
    -(void) add: (double) value
    { accumulator -= value; }

    then in the program itself, once object is made and the following lines:
    [deskCal setAccumulator: 100];
    [deskCalc add: 200];
    NSLog (@"the result if %6", [deskCalc accumulator]);

    so my confusion comes when you are declaring the add method as returning no value as i can see by -(void). But is this not returning the value atleast to the object that is receiving the method?

    so would (void) not return value in a sense that only the object that receives the method can access that value (private variable?); where as returning the value lets say (int) can return that value to a public variable that anyone can access?

    *Ive done examples that return nothing and return a value, but just want to make sure i understand this concept clearly.

    Thankyou!!
     
  2. Stella macrumors 604

    Stella

    Joined:
    Apr 21, 2003
    Location:
    Canada
    #2
    Your code extract doesn't show enough.. paste more of your classes.

    Try using:
    self.accumulator instead of just accumulator.

    -(void) add: (double) value
    { self.accumulator -= value; }

    You are subtracting from accumulator instead of adding... your method 'add' doesn't do what it advertises...

     
  3. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #3
    The OP's example should work as written, assuming accumulator is an ivar or global.

    Also, to the OP, the void means that your method returns nothing. That doesn't mean that it does nothing.

    All that returning means is that you can use it in leu of a variable on the right side of an assignment or as a function or method argument.

    IE, if add: returned something, you could do this:

    Code:
    round([deskCalc add:200]);
    or this:

    Code:
    myVar = [deskCalc add:200];
    But since it returns nothing, it can't be used in either of those ways. It can only be used in a statement by itself (as you used it in your example.)
     
  4. childoftheko4n, Jul 11, 2013
    Last edited by a moderator: Jul 11, 2013

    childoftheko4n thread starter macrumors regular

    Joined:
    Oct 18, 2011
    #4
    Oops! it should have definitely been accumulator += value. My apologizes for the typo. Also thankyou ArtofWarefare that cleared it up a lot actually. Do want to make sure though, so with an example like this (sorry if minor errors, trying to write this off top of my end).

    Code:
    @interface Calculator NSObject
    @property int accumulator;
    -(void) add: (int) n;
    @end
    
    @implementation Calculator
    @sythensize accumulator;
    -(void) add: (int) n
    {
      accumulator += n;
    {
    @end
    
    program:
    
    Calculator *myCalc = [[Calculator alloc] init];
    [myCalc add: 1];
    [myCalc add: 4];
    NSLog(@"%i", [myCalc.accumulator]);
    
    output should say:

    5

    correct? So with that in mind (and again, sorry for any other errors) because add method doesnt return a value, it can still be referenced and used in any way on the left side of the equation..but not th eright? like that is where i am confused. If that should work as a void method, what benefit would that example get from using a return value instead of void?
     
  5. dantastic macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #5
    Code:
    // this method will return nothing
    - (void) nothing;
    
    // this method will return an integer
    - (int) getMyInt;
    
    The return type is what the method produces. There are many methods that you will write that will just do something without returning a value at the end. Void means exactly that, it will return no value. The method implementation can be written like this

    Code:
    - (int) getMyInt {
       return 42;
    }
    
    - (void) nothing {
       return;
    }
    
    You would not normally include the return at the end of a void method but it's there really, it just returns nothing.

    So in your code snippet above your code will execute and then the method will return nothing and your piece of code that called the method will continue to execute.
     
  6. childoftheko4n, Jul 11, 2013
    Last edited by a moderator: Jul 11, 2013

    childoftheko4n thread starter macrumors regular

    Joined:
    Oct 18, 2011
    #6
    But see to me and the untrained eye some the programs i see that return nothing almost appear to be returning something. Here is a direct example from the book that works.
    Code:
    @implementation Fraction
    @property int numerator, denominator;
    
    -(void) print;
    -(void) setTo: (int) n over: (int) d;
    -(void) add: (Fraction *) f;
    @end
    
    @implementation Fraction
    @sythesize numerator, denominator;
    
    -(void) print
    {NSLog(@"%i/%i", numerator, denominator);
    
    -(void) setTo: (int) n over: (int) d
    {
       numerator = n;
       denominator = d;
    }
    
    -(void) add: (Fraction *) f
    {
       numerator = numerator * f.denominator + denominator *f.numerator;
       denominator = denominator * f.denominator;
    }
    @end
    
    Program:
    
    Fraction *aFraction = [[Fraction alloc] init];
    Fraction *bFraction = [[Fraction alloc] init];
    
    [aFraction setTo: 1 over: 4];
    [bFraction setTo: 1 over 2];
    
    [aFraction add: bFraction];
    [aFraction print];
    
    so here the program sets aFraction and bFraction to different values through a void method. If why use void method to add the fractions? Is the method not returning the results of the addition to aFraction?

    *this is probably the simplest thing but it leaves some grey spots in my understanding for some reason*


    **edit**

    with the example above, since add is returning no value, you couldnt do this could you?

    Fraction *cFraction = [Fraction alloc] init];
    cFraction = [aFraction add: bFraction];

    would that be an example of where void wouldnt do you any good, or would that be viable?
     
  7. dantastic macrumors 6502

    dantastic

    Joined:
    Jan 21, 2011
    #7
    Code:
    with the example above, since add is returning no value, you couldnt do this could you?
    
    Fraction *cFraction = [Fraction alloc] init];
    cFraction = [aFraction add: bFraction];
    
    would that be an example of where void wouldnt do you any good, or would that be viable? 
    No you can not. Not with your methods above.

    Before you ponder that one further have a read up on Object Oriented programming as a concept, nothing to do with objective c necessarily.
     
  8. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #8
    Those methods are doing something.

    They aren't returning anything.

    Think of return as a special variable that has the name "return", has an implied "=" sign after it, ends the function when it's reached, and has a type declared in the method signature. It can be accessed from other functions/methods by having its function/method called.

    Some functions/methods use their return variable to hand information back to whoever called it. Others don't.

    If I were writing a language, I'd probably find some alternative to having return... I'd probably have some kind of system of in variables and out variables.
     
  9. childoftheko4n thread starter macrumors regular

    Joined:
    Oct 18, 2011

Share This Page