0

I was writing a binary search function:

int binsearch(int seq[], int start, int end, int key)
{
    int len = end - start + 1;
    int mid = (start + end) / 2;

    if (len <= 0)
        return -1;
    else if (key == seq[mid])
        return mid;
    else if (key < seq[mid])
        binsearch(seq, start, mid - 1, key);
    else
        binsearch(seq, mid + 1, end, key);
}

I was not aware that I returned only once in the innermost call. I compiled it with gcc in my laptop, and it worked perfectly.

However, when I compiled that code in a cloud computer using clang, it threw me a warning that the control could reach the end of a non-void function. Then, I put another variable there to pass the return value over recursive calls.

Why did gcc compile it without any warning and yet the function gave the correct output? Is this a kind of compiler optimization? I thought C would not pass the return value automatically over recursive calls.

6
  • 4
    Not returning a value in a function declared to return a value leads to undefined behavior. Anything could happen, including seemingly working. Commented Nov 10, 2016 at 14:00
  • 3
    Looks like all you need to do is add a return keyword before the calls to binsearch in the final else if and else. Commented Nov 10, 2016 at 14:01
  • Side note: once you fix your code by adding a return preceding each call to binsearch, you can get rid of all the elses. Commented Nov 10, 2016 at 14:04
  • Your question is not clear. The error message otoh is. Commented Nov 10, 2016 at 14:05
  • @barakmanos: You are correct, but beware of the discussions about multiple return point in a function. Some people take coding standards like MISRA as a religion ... Commented Nov 10, 2016 at 14:07

2 Answers 2

2

The compiler says that these paths of the function

else if (key < seq[mid])
    binsearch(seq, start, mid - 1, key);
else
    binsearch(seq, mid + 1, end, key);

do not have return statements.

You need at least to write

else if (key < seq[mid])
    return binsearch(seq, start, mid - 1, key);
else
    return binsearch(seq, mid + 1, end, key);
Sign up to request clarification or add additional context in comments.

Comments

1

The behaviour of your program is undefined; you must explicitly return a value on all control paths.

A kind compiler will warn you of this; gcc certainly does if you set an appropriate warning level.

I believe that the fix in your case is to write return before your binsearch calls.

(The only exception to this is int main(...) where the compiler must introduce an implicit return 0; if it's missing).

1 Comment

I understand now. I compiled with gcc again by including -Wall and indeed it gave me the warning. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.