0

ok after much help i finally get what im supposed to be doing. this error code says i'm not returning a function and then tells me things i know are wrong...

scrabble.c:58:1: error: non-void function does not return a value in all control paths [-Werror,-Wreturn-type]
}
^

this error applies to this part of my code:

// define function to compute score
int compute_score(string word)
{

    int score= 0;
    for (int i=0, n= strlen(word); i <n; i++)
    {
        int index;
        for(int j=0; j<26; j++)
        {
            if(toupper (word[i])==(LETTERS[j]))
            {
                index = j;
            }
        }
            score+= POINTS[index];
            return score;
    }
}
1

First, remember that compilers are dumb. They check things that are theoretically possible.

The compiler looks at this code and considers the possibility that the outer for loop might not execute. If that were to happen, there will be no value returned.

The fix is to add a catchall return statement at the end of the function. The only thing you need to do now is decide what value to return. If this return were to execute, it would likely mean that something is dramatically wrong. Perhaps it should return a zero or negative value indicating a failure?

There's also the issue that with the return statement inside of the outer loop that the outer loop will only execute once.

BTW, what would happen if word was an empty string? Maybe the compiler isn't that dumb after all. ;-)

Put it all together, and moving the return statement outside the for loops solves the problem.

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

2

We talked about this before. Take the return statement out of your loops. You only need to have a value returned after all of the function has been performed. You are getting this error because the IDE wants a default return value in case the loop(s) never get executed. Since you only need a value returned after the loops have finished, you can solve this by moving the statement to the end of the function.

1
  • Kudos. I just added the explanation of the catchall. – Cliff B Mar 23 at 22:37

You must log in to answer this question.

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