Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Receiving "Control reaches end of non-void function" error message in Xcode

When adding this code to a simple calculator program I receive the error message "Control reaches end of non-void function".

Now after reading some response I know that I should be adding a return call so that the program doesn't hang up in the instance that the accumulator is not set but I want to know where to place it or which would be the proper way to phrase this.

Thanks, any help would be appreciated. I can provide the full program code if necessary as well.

-(double) changeSign
{
accumulator = -accumulator;
}

-(double) reciprocal
{
accumulator = 1/accumulator;

}

-(double) xSquared
{
accumulator = accumulator * accumulator;
}
like image 718
JonnyPolo Avatar asked Jul 11 '11 02:07

JonnyPolo


3 Answers

Very simply, each of the methods you have here is declared to return a double, but you never actually return anything.

You need to do something like:

-(double) changeSign
{
accumulator = -accumulator;
return accumulator;
}

Or, alternatively, if you don't intend to return the new value of accumulator, change the return type to void:

-(void) changeSign
{
accumulator = -accumulator;
}

ps. the issue is not that the program will "hang up in the instance that the accumulator is not set". It's that you are saying "Here a method that returns a double", but the code does not actually return anything. The error message means "I got to the end of this function that you promised would return something, but you seem to have forgotten".

like image 195
David Gelhar Avatar answered Oct 06 '22 00:10

David Gelhar


You need to return a double. The basic syntax of a method is

-(return type)methodName

If accumulator is a declared property, then your functions can return nothing, i.e. void, like this

-(void) changeSign
{
accumulator = -accumulator;
}

So then a call such as [self changeSign] will return nothing but will alter the value of accumlator.

Alternately, you can have a return type and return accumulator by

-(double) changeSign
{
return -accumulator;
}

Then you can do something like:

self.accumulator = [self changeSign];

I doubt the former is what your after, but hopefully it makes the syntax more clear.

like image 40
PengOne Avatar answered Oct 05 '22 23:10

PengOne


Your code indicates that these methods will each return a double:

- (double) reciprocal
{ // ^^ Return type
...

but none of them include return statements. You need to add those in order for values to be passed back to the caller:

- (double) reciprocal
{
    accumulator = 1 / accumulator;
    return accumulator;
}

Then in the calling code you can assign the result of this method to a variable:

double foo = [someObject reciprocal];

Or, if you don't intend to pass a value back (perhaps you are simply updating some internal state), change the return type to void.

- (void) reciprocal
{ // ^^ Indicates that there is no return value
...
like image 22
jscs Avatar answered Oct 06 '22 00:10

jscs