0

This seems pretty simple, but I'm not sure what I'm doing wrong. Thanks in advance for helping!

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

{

if (n >= 0) {

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

    }
}
else 
{
    return false;
}

}

The error says:

helpers.c:40:1: error: control may reach end of non-void function [-Werror,-Wreturn-type]

} ^ 1 error generated. make: *** [helpers] Error 1

1
  • Your code seems pretty much correct...just check whether it's the same code that you tried compiling, especially whether there is a 'else if' or 'else'...
    – abhishek
    Commented Jun 15, 2016 at 3:33

2 Answers 2

4

The error message you're getting means the compiler isn't sure if the function will ever end (i.e. that there will be a return value under every possible eventuality). The problem lies in the first "else" in your function. We all know that n is either >=0 or not, but the compiler doesn't. It thinks that your code hasn't covered all possible scenarios. Remove the "else", that way all bases are covered.

1

there's certainly a problem...two actually... first of all, why is your first condition including 0 as a possibility? surely there can't be a list of 0 members. secondly, you do not give your for loop a chance to run, because you say else, return false, meaning it only checks the first value. I would suggest doing away with the else part... PS. return means exit the current function with this value.... Good luck

You must log in to answer this question.

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