0

I know that this question has been asked before, but none of the answers have helped me. I keep getting the "control may reach end of non-void function" error when I try to make find after editing helpers.c.

bool search(int value, int values[], int n) {

if (n<0)
    return false;

    // TODO: implement a searching algorithm

for (int i = 0; i<n; i++)
{
    if (value == values[i])
        return true;
    if (value != values[i])
        return false;
}

}

I have tried making the 'if (value != values[i)' into just else. I have also tried making it into just return false without any if or else. But, I keep receiving the same error.

Thanks in advance for all the help.

1 Answer 1

0

Compilers are dumb. When the compiler looks at code, it sees that all of the returns are inside the for loop block. Since it's theoretically possible that the for loop may never execute (remember, compilers are dumb), it thinks that it's possible to execute the function without ever hitting a return. Add a catchall return false after the for loop block, and just before the final closing curly brace, and it should compile.

BTW, here's what could be the definitive explanation in this forum: How to solve "control may reach end of non void function" error

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • Thank you so much! Jul 3, 2016 at 18:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .