#include
using namespace std;
int FIB(int m) <
if (m == 0)
return 4;
else if (m == 1)
return 7;
else if (m >= 2)
return (2* FIB(m-1) + 5 * FIB(m-2)) %2011;
>
int main() <
int t;
cin >> t ;
for (int i = 0; i > m;
cout Голосование за лучший ответ
I’ve been getting strange compiler errors on this binary search algorithm. I get a warning that control reaches end of non-void function . What does this mean?
5 Answers 5
The compiler cannot tell from that code if the function will ever reach the end and still return something. To make that clear, replace the last else if(. ) with just else .
The compiler isn’t smart enough to know that , > , and == are a «complete set». You can let it know that by removing the condition «if(val == sorted[mid])» — it’s redundant. Jut say » else return mid; «
Always build with at least minimal optimization. With -O0 , all analysis that the compiler could use to determine that execution cannot reach the end of the function has been disabled. This is why you’re seeing the warning. The only time you should ever use -O0 is for step-by-line debugging, which is usually not a good debugging approach anyway, but it’s what most people who got started with MSVC learned on.
I had the same problem. My code below didn’t work, but when I replaced the last «if» with «else», it works. The error was: may reach end of non-void function.
add to your code:
at the end of main()

Not the answer you’re looking for? Browse other questions tagged c warnings compiler-warnings or ask your own question.
Linked
Related
Hot Network Questions
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2019 Stack Exchange Inc; user contributions licensed under cc by-sa 4.0 with attribution required. rev 2019.11.15.35459
a pCj d JOmvg sLWK b E y zpdSs l J KIFiA e rxR t tc B Enafr r o a Ut i jcSWI n l s N
Answer Wiki
![]()
Why does my compiler sometimes say: «control reaches end of non-void-function»?
Because you forgot the return value.
Sometimes compilers get confused by conditionals and don’t realise that every possible branch results in a return value. For example
might generate this error, because the compiler doesn’t recognise that all branches return a value.