c++ - Type No return, in function returning non-void -
my c++ code looks this:
int f(int i){ if (i > 0) return 1; if (i == 0) return 0; if (i < 0) return -1; }
it's working still get:
warning: no return, in function returning non-void
even though obvious cases covered. there way handle in "proper" way?
the compiler doesn't grasp if
conditions cover possible conditions. therefore, thinks execution flow can still fall through past if
s.
because either of these conditions assume others false, can write this:
int f(int i) { if (i > 0) return 1; else if (i == 0) return 0; else return -1; }
and because return
statement terminates function, can shorten this:
int f(int i) { if (i > 0) return 1; if (i == 0) return 0; return -1; }
note lack of 2 else
s.
Comments
Post a Comment