mercredi 19 juillet 2017

Control may reach non void function ???? in c++ why ????

I'm trying to write a simple function in c++

double r2force(int vel){
    // corver velocity
    int corner_v = 400;
    // steady state
    double F_ss = 28.23;
        // if vel is smaller than the corner velocity the force that
        if ( vel < corner_v){
            return F_ss;
        }
        else if ( vel >= corner_v){

             double a = 12.57;
             double b = -4.483;
             double c = 4/4;

            return a*log10(vel*c)+b;
        }
}

but this gives me the "Control may reach non void function" error. I can't think on top of my head in which situation you would not go into the if statment??? so I actually added

double r2force(int vel){
    // corver velocity
    int corner_v = 400;
    // steady state
    double F_ss = 28.23;
        // if vel is smaller than the corner velocity the force that
        if ( vel < corner_v){
            return F_ss;
        }
        else if ( vel >= corner_v){

             double a = 12.57;
             double b = -4.483;
             double c = 4/4;

            return a*log10(vel*c)+b;
        }
        else{
            return NAN;
        }
}

and now this works. I added NaN thinking what if the function takes in something besides int but since the function is defined to take int only, I don't think that's the right rationale. Why does my second code works and not the first ?

Aucun commentaire:

Enregistrer un commentaire