Reputation: 1249
I am wondering if the return statement is immediate when a condition occurs without any exception.
In particular I've a BOOL function:
bool pm2_filter( std::string gnomad_ex_controls_an, std::string gnomad_gen_controls_an, std::string &gene_inh_mode )
{
if ( gnomad_ex_controls_an == "NA" && gnomad_gen_controls_an == "NA" ) {
return true;
}
else {
if ( gene_inh_mode == "dom" || gene_inh_mode == "NA" ) {
if ( gnomad_ex_controls_an != "NA" ) {
if (std::stoi(gnomad_ex_controls_an) == 0) {
return true;
}
}
else if ( gnomad_gen_controls_an != "NA" ) {
if (std::stoi(gnomad_gen_controls_an) == 0) {
return true;
}
}
else {
return false;
}
}
else if ( gene_inh_mode == "rec" ) {
if ( gnomad_ex_controls_an != "NA" && floatable(gnomad_ex_controls_an) ) {
if (cmpf(std::stof(gnomad_ex_controls_an), 1E-4, 1E-10)) {
return true;
}
}
else if ( gnomad_gen_controls_an != "NA" && floatable(gnomad_gen_controls_an) ) {
if (cmpf(std::stof(gnomad_gen_controls_an), 1E-4, 1E-10)) {
return true;
}
}
else {
return false;
}
}
}
}
If I try to run it in this manner it warnings me:
dependencies/filterFunctions.cpp:403:1: warning: control may reach end of non-void function [-Wreturn-type]
}
to avoid the warning I can put a final return
to the function, e.g.
bool pm2_filter( std::string gnomad_ex_controls_an, std::string gnomad_gen_controls_an, std::string &gene_inh_mode )
{
if ( gnomad_ex_controls_an == "NA" && gnomad_gen_controls_an == "NA" ) {
return true;
}
else {
if ( gene_inh_mode == "dom" || gene_inh_mode == "NA" ) {
if ( gnomad_ex_controls_an != "NA" ) {
if (std::stoi(gnomad_ex_controls_an) == 0) {
return true;
}
}
else if ( gnomad_gen_controls_an != "NA" ) {
if (std::stoi(gnomad_gen_controls_an) == 0) {
return true;
}
}
else {
return false;
}
}
else if ( gene_inh_mode == "rec" ) {
if ( gnomad_ex_controls_an != "NA" && floatable(gnomad_ex_controls_an) ) {
if (cmpf(std::stof(gnomad_ex_controls_an), 1E-4, 1E-10)) {
return true;
}
}
else if ( gnomad_gen_controls_an != "NA" && floatable(gnomad_gen_controls_an) ) {
if (cmpf(std::stof(gnomad_gen_controls_an), 1E-4, 1E-10)) {
return true;
}
}
else {
return false;
}
}
}
// FINAL RETURN:
return false;
}
but I'm wondering if this return
at the end of the function is effectively returned ONLY ADN IF ONLY NO other previous return
is met, without exception.
I tried to figure it out on the web nut found no specific answer, so I'm asking you expert guys.
Thanks a lot in advance for any help.
Upvotes: 0
Views: 85
Reputation: 180630
Your assumption is correct.
// FINAL RETURN:
return false;
Will only run if none of the other return
's are reached. The reason you get the warning is if
if ( gnomad_ex_controls_an == "NA" && gnomad_gen_controls_an == "NA" )
is false then you go into the else part and if
if ( gene_inh_mode == "dom" || gene_inh_mode == "NA" )
is false then you go to
else if ( gene_inh_mode == "rec" )
and if that is false then you fall out of all the condition and reach the end of the function. It is undefined behavior to not return from a function that is supposed to return something so you get an error. It may be that it is impossible for your data set to reach that, but the compiler can't know that so it warns you.
Upvotes: 1