When defining a function in C, I get the error message "control can reach the end of a non-void function" in the compiler when writing if / else logic in a certain way (script 1 below), but I don't get an error when writing logic in a different way (script 2 below). For me, both ways of writing this function seem similar, so I donβt understand why version 1 will not compile.
Scenario 1
bool search(int value, int values[], int n) { int i; if (n<1) { return false; } for(i=0;i<n;i++) { if (value==values[i]) { return true; } else { return false; } } }
Scenario 2
bool search(int value, int values[], int n) { int i; if (n<1) { return false; } for(i=0;i<n;i++) { if (value==values[i]) { return true; } } return false; }
Would script 2 always return false after a for loop? Or is the function essentially βStopβ after returning the value for the first time, so it returns βtrueβ after the value matches [i]?
source share