0

I'm getting error non-void function search should return a value. If I add anything else to last return, such as false 1 or zero, the errors multiply. I've tried moving the indentations as well as adding and removing parenthesis. Can anyone advise me, please?

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

    return;
    }
}

1 Answer 1

1

Having the errors multiply shouldn't necessarily be looked at as the code getting worse. Rather, the new errors should be analyzed for what they are. Sometimes an error masks other errors, other times, an error causes other errors. The reality here is that you have both conditions happening. First, the return issue is being detected by the compiler and subsequent errors are being masked. The other errors are still there, despite being masked by this error and you will have to cure them too. At least one of them will show multiple error messages for a single problem because this single error is causing a cascading syntax issue.

So, since you have a function with a return type of bool, every return in the code must return a boolean value. Best practice is to return true or false, not a number.

Once that's fixed, there are more problems, including a curly brace mismatch - something that often precipitates multiple error messages for a single error, but I'll let you have a chance to work through them on your own first. ;-) This is something that you should try to do on your own because it is a very important skill to master. If you do it without assistance, you'll learn a lot more and a very valuable skill. If, however, you can't resolve all of the bugs, then post a new question with the updated code and a description of where you are with it.

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

You must log in to answer this question.

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