38

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;
}

11 Answers 11

55

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.

4
  • 6
    (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
    Commented May 30, 2011 at 1:18
  • thank you! that makes sense! won't that catch something unwanted, though?
    – tekknolagi
    Commented May 30, 2011 at 1: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
    Commented May 30, 2011 at 1: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
    Commented Jul 8, 2018 at 17:34
16

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;"

4
  • The compiler is smart enough. The problem is that the user was not smart enough to specify a non-zero optimization level. -O0 is almost always a very very bad idea. Commented May 30, 2011 at 2:24
  • Interesting idea, but I think not. For example, gcc gives this error with -O3 . Commented May 30, 2011 at 2:44
  • 1
    Indeed, I confirmed with gcc 4.5.2. Commented May 30, 2011 at 3:02
  • 1
    Further, I tested adding a printf call at the end of the function, and gcc actually generates code for it. So somehow it's not detecting this point as unreachable... Commented May 30, 2011 at 3:04
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.

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...

3
  • 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? Commented May 30, 2011 at 2: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... Commented May 30, 2011 at 2: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. Commented May 30, 2011 at 3:04
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'; 
        }

  }
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()

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.

-6

add to your code:

"#include < stdlib.h>"

return EXIT_SUCCESS;

at the end of main()

2
  • This worked although it is downvoted. This makes sure that the function returns something so that the compiler won't raise errors.
    – dilanSachi
    Commented May 16, 2020 at 5: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
    Commented Mar 7, 2021 at 16:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.