Amazon.com Widgets I don't understand the logic
Welcome, Guest. Please login or register.
Did you miss your activation email?
September 16, 2014, 02:25:51 PM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Old Stuff
| |-+ Answers to Exercises
| | |-+ Chapter 4
| | | |-+ I don't understand the logic
Pages: [1] Go Down
Print
Author Topic: I don't understand the logic (Read 1378 times)
Michel
Newbie
*
Posts: 7






on: August 31, 2011, 07:04:58 AM

Hi!

In exercises 8, 9 and 10 we are asked to let the methods return the value of the accumulator.

if we look at for example the changeSign method from exercise 9, what is the difference between:

THIS

Code: (Objective-C)
-(void) changeSign
{
    accumulator *= -1;
}

AND THIS?

Code: (Objective-C)
-(double) changeSign
{
    return accumulator *= -1;
}

Both work fine in the program. So what is the reason/logic for letting the method return the accumulator?

Cheers! Smiley

p.s.: I did a forum search of course but that was not of any help.
Last Edit: August 31, 2011, 07:13:34 AM by Michel Logged
Waiting
Global Moderator
Full Member
*****
Posts: 113






Reply #1 on: August 31, 2011, 08:36:47 AM

If you don't return the value then you would have to make another call to get the value.

EG, if you called in an nslog statement then you would set the new value and it would be returned to display in the statement.

If you do it without returning a value, then you would call that function before the nslog statement, then make another call in the nslog statement to return the value, so having to write extra methods and extra code in the program for the same result.
Logged
Michel
Newbie
*
Posts: 7






Reply #2 on: September 01, 2011, 02:21:21 AM

Thanks "Waiting"!

But I still don't really get it.

Why do we need to return a value? Why is it not enough to simply change the value of the accumulator?
Logged
Casey
Newbie
*
Posts: 2






Reply #3 on: October 30, 2011, 03:56:26 PM

I agree with Michel here.  Adding the 'return' method to any of the arithmetic methods does NOT seem to return the value in the resulting statement after you run the code.  All the solutions I've seen offered up on this forum include a separate NSLog string, using the 'accumulator' method to specify the %g variables, after the arithmetic methods in their code in order to bring up the intermediate result.

For example the following two lines of code:

[deskCalc add: 10];
NSLog (@"Adding 10 gives %g", accumulator);

yield only:

Adding 10 gives 20

and NOT:

20
Adding 10 gives 20


which is what you would expect if the 'return' method added to the original arithmetic method definitions actually returned a value.  What you get instead is exactly what you would get if you left the arithmetic methods as 'void' and omitted the 'return' method.  Therefore, it seems that adding the 'return' method to the arithmetic methods is redundant and unnecessary, and hence, illogical.

If we're missing something can anyone sort this out for us?
Logged
Waiting
Global Moderator
Full Member
*****
Posts: 113






Reply #4 on: October 30, 2011, 06:49:06 PM

I agree with Michel here.  Adding the 'return' method to any of the arithmetic methods does NOT seem to return the value in the resulting statement after you run the code.  All the solutions I've seen offered up on this forum include a separate NSLog string, using the 'accumulator' method to specify the %g variables, after the arithmetic methods in their code in order to bring up the intermediate result.

For example the following two lines of code:

[deskCalc add: 10];
NSLog (@"Adding 10 gives %g", accumulator);

yield only:

Adding 10 gives 20

and NOT:

20
Adding 10 gives 20


which is what you would expect if the 'return' method added to the original arithmetic method definitions actually returned a value.  What you get instead is exactly what you would get if you left the arithmetic methods as 'void' and omitted the 'return' method.  Therefore, it seems that adding the 'return' method to the arithmetic methods is redundant and unnecessary, and hence, illogical.

If we're missing something can anyone sort this out for us?

Because its easier to just do

NSLog (@"Adding 10 gives %g",[deskCalc add: 10]);
Logged
Casey
Newbie
*
Posts: 2






Reply #5 on: October 31, 2011, 07:02:33 PM

Ohhh now I get it!

so instead of

[deskCalc add: 50]
NSLog (@"The answer is %g",[deskCalc accumulator]);

you can just write

NSLog (@"The answer is %g",[deskCalc add:50]);

That makes a lot of sense, thanks!  Grin
Logged
Waiting
Global Moderator
Full Member
*****
Posts: 113






Reply #6 on: October 31, 2011, 07:27:04 PM

No problem.
Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.