Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Why is xcode ignoring a NSLog statement placed in sum definition?

The output panel displays the second NSLog statement in the very bottom of the text, but not the first one located within the sum method definition.

I noticed that when I place the NSLog statement before the "return accumulator," the statement appears, but when it's placed after the return, as in this case, it doesn't.

Why is that?

#import <Foundation/Foundation.h>  

// interface  
@interface Calculator: NSObject  

//accumulator methods  
-(void) setAccumulator: (double) n;  
-(void) clear;  
-(double) accumulator;  

//arithmetic methods  
-(double) add:       (double) n;  
-(double) subtract:  (double) n;  
-(double) divide:    (double) n;  
-(double) multiply:  (double) n;  

//memory methods  
-(double) memoryClear;                  // clear memory  
-(double) memoryStore;                  // set memory to accumulator accu <= memory  
-(double) memoryRecall;                 // retrive memory from accumulator memory <= accu  
-(double) memoryAdd: (double) value;    // add value into memory  
-(double) memorySubtract: (double) value; // subtract value from memory   

@end  

//implementation  
@implementation Calculator  
{  
    double accumulator, memory;  
}  

-(void) setAccumulator: (double) n  
{  
    accumulator = n;  
}  

-(void) clear;  
{  
    accumulator = 0;  
}  

-(double) accumulator  
{  
    return accumulator;  
}  

-(double) add:(double)n        
{  

    accumulator += n;  
    return accumulator;  
    NSLog(@"The sum of %g and %g is %g", accumulator, n, accumulator+n); 
}  

-(double) subtract:(double)n  
{  
    accumulator -=n;  
    return accumulator;  
}  

-(double) divide: (double) n  
{  
    accumulator /=n;  
    return accumulator;  
}  

-(double) multiply: (double) n  
{  
    accumulator *=n;  
    return accumulator;  
}  

-(double) memoryClear  
{  
    memory = 0;  
    return memory;   
}  
@end  

//program  
int main(int argc, const char * argv[])  
{  

    @autoreleasepool {  
        Calculator *myCalc = [[Calculator alloc] init];  
        [myCalc setAccumulator:100];  
        [myCalc add:30];  
        [myCalc subtract:30];  
        [myCalc divide:2];  
        [myCalc multiply:3];  
        NSLog(@"Hello, World! %g", [myCalc accumulator]);  


    }  
    return 0;  
}  

1 Answer

This is because return instantly returns, it doesn't execute any other code in the function.

You can think of return as 'exit function'.