Guigui
Guigui

Reputation: 39

Warning : "Not all control paths return a value" c++

I don't understand this warning for my little function:

int Fct_name (int nb1, int * nb2) 
{
    switch (Dest)
    { 
    Case 1 : 
        return Fct_1(nb1,nb2);
    Case 2 :
        return -1;
    }
}

If someone can help me?

Upvotes: 0

Views: 5074

Answers (6)

lostbard
lostbard

Reputation: 5220

Add a return XXX at the end of the function which will then ensure that the compiler will not have anyway of getting to the end of the function without a return of value occurring.

Upvotes: -1

raullalves
raullalves

Reputation: 856

If Dest can only be 1 or 2, you could rewrite like this

int Fct_name (int nb1, int * nb2) 
{
    if(Dest == 1) return Fct_1(nb1,nb2);
    return -1;
}

Upvotes: 0

97amarnathk
97amarnathk

Reputation: 1035

As said by @NathanOliver, the warning means that there might be a possible case where your function might not return any value. To be more precise if a user inputs a value which is not 1 or 2, then your function does not return any value.

You might be thinking that you are only going to input 1 or 2. But the compiler doesn't know this.

What you can do is -

  1. Ignore the warning - You can ignore the warning and go right ahead. But be careful that you only put 1 o 2 as the parameter in all cases.

Though I would not recommend ignoring the warning. In general, it is better to pay heed to the warnings. In the long run, it saves you from many bugs in case you are working on big projects.

  1. Add a default - This condition will not actually execute ever, but the compiler will now stop giving the warnings.

Here's the corrected code -

int Fct_name (int nb1, int * nb2) 
    {
        switch (Dest)
        { 
           case 1 : 
              return Fct_1(nb1,nb2);
           case 2 :
              return -1;
           default:
             return 0;
       }
   }

Or you could do this -

int Fct_name (int nb1, int * nb2) 
{
    switch (Dest)
    { 
       case 1 : 
          return Fct_1(nb1,nb2);
       case 2 :
          return -1;
    }
    return 0;
}

Upvotes: 0

Stephan Lechner
Stephan Lechner

Reputation: 35154

If Dest is really an enum as you said, the compiler should not issue a warning in your case. At least my compiler does not. So the following code compiles without warning/error:

enum {
  x = 1,
    y = 2
} Dest;

int Fct_name (int nb1, int * nb2)
{
    switch (Dest)
    {
            case 1 :
            return -5; //Fct_1(nb1,nb2);
            case 2 :
            return -1;
    }
}

int main() {

}

However, if Dest is an integer or if the enum defines other values than 1 and 2, then I get an error as well. This can then be fixed with a default-path:

enum {
  x = 1,
    y = 2,
    z = 3
} Dest;

int Fct_name (int nb1, int * nb2)
{
    switch (Dest)
    {
            case 1 :
            return -5; //Fct_1(nb1,nb2);
            case 2 :
            return -1;
        default:
            return 0;
    }
}

Upvotes: 1

Aganju
Aganju

Reputation: 6395

If dest is an enum with only two values, it is unnecessary complicated - return Dest == 1 ? Fct_1(nb1,nb2) : -1; is enough. Otherwise, replace case 2: with default:.

Upvotes: 0

Hatted Rooster
Hatted Rooster

Reputation: 36483

It's because, as the warning says, not all paths of your code return a value while the function has a distinct return type which tells the compiler "hey, I'm going to return something." but you don't actually do that if Dest is anything other than 1 or 2.


You commented:

Dest can only be 1 or 2 (it's an enum)

Yes okay but only you know that, your compiler doesn't, and it won't take your word for it. It can only see the static properties of your code, it can't predict how the runtime will go and thus it won't accept your code. For all it knows Dest can be changed by an external piece of code etc etc.


You should add some sort of default value:

int Fct_name (int nb1, int * nb2) 
{
   switch (Dest)
   { 
    case 1 : 
         return Fct_1(nb1,nb2);
    case 2 :
         return -1;
    }
    return 0;
}

Upvotes: 2

Related Questions