0

What is the meaning of return statement in Objective-C>?

For example, a simple calculator method to add numbers:

-(double) add: (double) value
{
    accumulator += value;
    return accumulator;
}

Does it mean that the result will be returned (or stored?) back to accumulator? What if there would be no return statement in that case?

Also, how to explain the statement "return 0;" that we write in the end of each program.

Thanks in advance.

1
  • 6
    I strongly suggest a [C] tutorial/book which should cover all the standard syntax constructs and expressions and explain statements and order of operations, etc :) I have fond memories of the K&R Book. Objective-C is just an extension ... return (and +=) is just "normal" C. Commented Feb 18, 2012 at 8:22

5 Answers 5

6

The value given in the return statement is the value returned by the function or method. In your example, the value in accumulator will be the result of calling the method -add:, like this:

double bar = [foo add:3.1];

bar will get the value that was in accumulator.

Also, how to explain the statement "return 0;" that we write in the end of each program.

In Unix (and remember, both MacOS X and iOS are flavors of Unix), programs return a value when they exit. The value 0 indicates normal termination; other values indicate some sort of error or abnormal termination.

Sign up to request clarification or add additional context in comments.

8 Comments

But what if I wouldn't write return accumulator in my method definition, wouldn't "bar" still store the value of addition?
No, it wouldn't, because there would be no result. The result is exactly that value provided in the return statement. Also, your compiler would complain about execution reaching the end of a non-void method or function. If you declare a method as returning void, you don't need a return statement because void indicates that there's no result. However, you can still use return without any argument in a method or function declared with void return type -- it's often used to return early, for example. See any decent C text for a complete description.
to your addition in your original post: since we are writing return 0, doesn't we force it to terminate normally then?
What if we had -(void) add: (double) value, will then bar store the value?
Yes, if execution gets as far as the return statement in main(), the program will exit with 0 as the exit status, which means normal termination. No problem there. If you had a reason to indicate some other exit status (usually 1 for failure), you could return that instead.
|
2

Your simple code does two things. It changes the value of accumulator, then it returns a copy of that value to wherever the function was called.

If the function was called like:

float x = [self add:2.0];

x would become whatever the return statement says.

Because you are changing an instance variable I doubt you need to make the return call and the function could return void instead of double:

-(void)add:(double)value;

But I guess there could be a case where you wanted to update an instance variable and also return the same value.

Comments

1

return is a very standard statement in pretty much all programming languages.

It'll "return" the value to it's right to the method/function that called it.

Please read these:

http://en.wikipedia.org/wiki/Return_statement

and

https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Introduction/introObjectiveC.html

Comments

0

No, it does not modify accumulator. It returns a value (copied from accumulator) from the message. This means it provides the value to whatever code called the message.

Comments

0

If whoever or whatever called this method or program does not want or use the result of the call, then the return statement does nothing except exit the called method or program after computing the result ( if it hasn't already).

If the caller of a function, method, or program does want the result (for an assignment or use in an equation, for instance) then the return statement says what that result value is.

A program is really a function or method called by the OS, so it can return a value to the OS. In Terminal, type echo $status to see the last returned result from a shell command.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.