Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm not sure as to why I'm getting this error. I checked the brackets and parentheses. The loop shouldn't continue forever.

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

PS: I'm required to use Werror in my case.

Thanks

What I have tried:

C#
bool search(int value, int values[], int n) 
{
 if ( n < 0)
 { return false; }

for (int a = 0; a < n; a++)
{
    if ( values[a] == value)
    {
        return true;
        break;
    }

    else
    {
        if ( a == n)
        {
            return false;
        }
    }
}
}
Posted
Updated 28-Dec-16 8:31am
v2
Comments
[no name] 28-Dec-16 12:51pm    
What is your function going to return if n = 0?
Member 12919791 28-Dec-16 12:55pm    
Yes I didn't remember that.

I now changed the code at the beginning to consider if (n <= 0).

Yet the same error comes.

Thanks a lot,
cvogt61457 28-Dec-16 14:26pm    
The compiler is smart but there are situations which can trick it.
Your code looks as though it can exit with no return - therefore, you have the warning.
The loop might actually complete and exit with the
if (a == n)
but the compiler can't figure that out.
Richard MacCutchan's solution looks nice and easy to read.
Note:
Clarity is highly valued when writing code. Maintenance is much easier and others can gain an understanding of
what you're wanting to do much easier.
KISS - Keep It Simple Stupid

Your search function requires a return statement at the end. Also, you do not require a break statement after a return. In fact you do not need the first if or the else clause at all, all you need is:
C++
bool search(int value, int values[], int n) 
{
    for (int a = 0; a < n; a++)
    {
        if ( values[a] == value)
        {
            return true;
        }
    }
    return false;
}
 
Share this answer
 
Your code inside the for loop is chaotic at best and doesn't need the else clause of the if statement. Your function doesn't return a value on all code paths. What happens if the loop completes without satisfying any of the conditions you set in those if statements? The compiler cannot figure that out and cannot assume the for loop will never complete.

You should be rewriting this code to set a return value instead of just straight out returning from inside the loop. For example:
C
bool search(int value, int values[], int n) 
{
    // The default is to return false.
    bool returnValue = false;

    // Is the n parameter OK?
    if ( n >= 0)
    {
        // Yes, so execute the search.
        for (int a = 0; a < n; a++)
        {
            if (values[a] == value)
            {
                returnValue = true;
                break;
            }
        }
    }

    return returnValue;
}
 
Share this answer
 
v2
Quote:
Error: helpers.c:43:1: error: control may reach end of non-void function [-Werror,-Wreturn-type] } ^

You get this error message because it is possible to reach the end of the function without encountering a return
C++
for (int a = 0; a < n; a++)
{
    if ( values[a] == value)
    {
        return true;
        break;
    }

    else
    {
        if ( a == n)
        {
            // This line will never execute because a is never equal to n inside the loop
            return false;
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900