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;
}
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".
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.
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
...
The thing is, when we use return type other than "void" we should return something to the calling function for example:
(returnType)methodName
{
---required code---
return something(as;-1/0/1/particular Value);
}
If You have define as
-(void)methodName{
//No need to return any thing.
}
if You have write any return type.
-(int)methodName{
return int
}
Example
-(void)getData{
//Write any thing without return.
}
-(int)getData{
int total = a + b;
return total;
}
Or
-(int)getData{
return a + b;}
精彩评论