0

I know this has been asked many times before, but I looked through all the questions and still don't get it. I know what the error means, I'm just convinced that my code ensures that whatever happens, the function will return something

Here's the code (it's for helpers.c in pset3):

#include <cs50.h>
#include "helpers.h"

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

I will really appreciate any help, as this is driving me absolutely crazy :)

1
  • Can you please edit your question and include all of the code for the function that has this? While this may be fine, there is no way to know why the compiler isn't happy without seeing the whole function. Remember, compilers are dumb. They can't and don't see all the nuances of logic. All they know is that if there appears to be a path that reaches the end of a function without a return statement, they complain. – Cliff B Jul 29 '15 at 19:49
2

Consider following question: what happens, if there is no value in values[]?

In such case, assuming n is greater than 0, we would never reach any of the return statements:

  1. First one wouldn't get executed because of the condition:

    if(values[i] == value) 
    

    which is never true in our case.

  2. Second one also wouldn't get executed, since n doesn't change its value and is greater than 0 in our example - we would never reach else statement and thus return one inside.

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