This is because your function has a possible code path that lets the if’s fall through without the function returning anything. Technically it shouldn’t be possible, but the compiler has noticed the possibility, and won’t let you continue. Your function should look more like this:
Degree climate_control(Degree degree) {
if (degree == LOW_TEMPERATURE_BOUNDARY) {
return 0;
} else if (degree < LOW_TEMPERATURE_BOUNDARY) {
return LOW_TEMPERATURE_BOUNDARY; }
else if (degree == HIGH_TEMPERATURE_BOUNDARY) {
return 0;
} else if (degree > HIGH_TEMPERATURE_BOUNDARY) {
return HIGH_TEMPERATURE_BOUNDARY; }
return 0;
}
Why does the compiler think this? What would happen to the above code if some brain-dead (or drunk) programmer did this:
const Degree LOW_TEMPERATURE_BOUNDARY = 18.5;
const Degree HIGH_TEMPERATURE_BOUNDARY = -22.2; //Notice the sign change?
Now your climate_control
function will fall through.
0
solved C error: control reaches end of non void function