36

I've been getting strange compiler errors on this binary search algorithm. I get a warning that control reaches end of non-void function. What does this mean?

int binary(int val, int sorted[], int low, int high) {
    int mid = (low+high)/2;

    if(high < low)
        return -1;

    if(val < sorted[mid])
        return binary(val, sorted, low, mid-1);

    else if(val > sorted[mid])
        return binary(val, sorted, mid+1, high);

    else if(val == sorted[mid])
        return mid;
}
detly
  • 29,332
  • 18
  • 93
  • 152
tekknolagi
  • 10,663
  • 24
  • 75
  • 119

11 Answers11

53

The compiler cannot tell from that code if the function will ever reach the end and still return something. To make that clear, replace the last else if(...) with just else.

rid
  • 61,078
  • 31
  • 152
  • 193
  • 5
    (And the reason the compiler is scared is that a function typed to return an `int` must actually return an `int`, and not just fall off the end of the function) – marnir May 30 '11 at 01:18
  • thank you! that makes sense! won't that catch something unwanted, though? – tekknolagi May 30 '11 at 01:18
  • 3
    @tekknolagi, well, the logic for us humans is quite clear: `val` can only be `<`, `>`, or `==` to `sorted[mid]`, there's really no alternative... So it can't catch anything else if that's the whole logic. – rid May 30 '11 at 01:21
  • 2
    Another way to get rid of the warning is to add `assert(false);` at the end of the function, to clearly tell the compiler (and human reader) that you deliberately omitted the _else_ case because you are sure it cannot happen. – dpi Jul 08 '18 at 17:34
15

The compiler isn't smart enough to know that <, >, and == are a "complete set". You can let it know that by removing the condition "if(val == sorted[mid])" -- it's redundant. Jut say "else return mid;"

Ernest Friedman-Hill
  • 80,601
  • 10
  • 150
  • 186
1

Error image

If the function is non-void,it means it has to return something before reaching the end of function block[ _} ].So, when we give only if and else-if statements the compiler cannot tell from that code,that any of these statements will be evaluated to true and return something.Means, if all of the condition evaluates to false,then the control will reach the end of function,without returning something,which is wrong.

Srijoy_paul
  • 77
  • 1
  • 6
0

I had the same problem. My code below didn't work, but when I replaced the last "if" with "else", it works. The error was: may reach end of non-void function.

int shifted(char key_letter)
  {
        if(isupper(key_letter))
        {
            return key_letter - 'A'; 
        }

        if(islower(key_letter)   //<----------- doesn't work, replace with else

        {                                            


            return key_letter - 'a'; 
        }

  }
Thuy
  • 1,493
  • 1
  • 11
  • 11
0

Always build with at least minimal optimization. With -O0, all analysis that the compiler could use to determine that execution cannot reach the end of the function has been disabled. This is why you're seeing the warning. The only time you should ever use -O0 is for step-by-line debugging, which is usually not a good debugging approach anyway, but it's what most people who got started with MSVC learned on...

R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711
  • I am skeptical of this. `gcc` 4.0.1 gives this warning with optimization cranked all the way up. Do you know of a specific compiler where optimization would make this message go away? – Ernest Friedman-Hill May 30 '11 at 02:47
  • I haven't tried this specific code, but in general gcc is good about detecting unreachable code. I wonder if there's some arcane aliasing consideration that's making it impossible to guarantee that the end of the function is not reachable... – R.. GitHub STOP HELPING ICE May 30 '11 at 02:59
  • 1
    Indeed. I'd be happy to hear what's going on. This has actually turned into a rather interesting question about gcc's behavior. – R.. GitHub STOP HELPING ICE May 30 '11 at 03:04
0

Make sure that your code is returning a value of given return-type irrespective of conditional statements

This code snippet was showing the same error

int search(char arr[], int start, int end, char value)
{
    int i;
    for(i=start; i<=end; i++)
    {
        if(arr[i] == value)
            return i;
    }
}

This is the working code after little changes

int search(char arr[], int start, int end, char value)
{
    int i;
    int index=-1;
    for(i=start; i<=end; i++)
    {
        if(arr[i] == value)
            index=i;
    }
    return index;
}
0

It means it's searching for a function that needs to be completed.

else if(val == sorted[mid]) return mid;

so, remove the if() part and modify the code or add an else() at the end which returns an int.

0

Compiler by itself would not know that the conditions you have given are optimum .. meaning of this is you have covered all cases .. So therefore it always wants a return statement ... So either you can change last else if with else or just write return 0 after last else if ;`int binary(int val, int sorted[], int low, int high) { int mid = (low+high)/2;

if(high < low)
    return -1;

if(val < sorted[mid])
    return binary(val, sorted, low, mid-1);

else if(val > sorted[mid])
    return binary(val, sorted, mid+1, high);

else if(val == sorted[mid])
    return mid;
return 0; }`
0

If it's "main" function just make sure to return 0 or change it from int main() to void main()

Niv
  • 850
  • 1
  • 7
  • 22
0

Add the binary function to the end of the code:

int binary(int val, int sorted[], int low, int high)
{ /* ... */ return 1;}

If not one of the conditions is not met, the int function should return int.

user16217248
  • 3,119
  • 19
  • 19
  • 37
Vladimir
  • 1
  • 1
-6

add to your code:

"#include < stdlib.h>"

return EXIT_SUCCESS;

at the end of main()

Ram Sharma
  • 8,676
  • 7
  • 43
  • 56
Donitsky
  • 7
  • 1
  • This worked although it is downvoted. This makes sure that the function returns something so that the compiler won't raise errors. – dilanSachi May 16 '20 at 05:48
  • this problem can cause this error, but this question is not about `main()`, it is about a different form of the error. other questions on stackoverflow do talk about this error in `main()`. – fuzzyTew Mar 07 '21 at 16:28