the compiler said the function CandleBelowMa40(),
"not all control paths return a value",
why?
Now, imagine that for whatever reason, when you enter the "CandleBelowMA40" function, i = -1. Maybe you don't expect that to happen, but it might. That means that the "for" loop won't be executed, and you'll get to the end of the function without having a value to return. Also, maybe the flow enters the "for", but what would happen if "Open[i]" is equal to "Close[i]"? You won't enter the "if" nor the "else", am I right? That's what that error means. You have to control all paths, and make sure you ALWAYS return a value if your function is meant to return a value. If you don't expect "i" to be -1, then just put at the end of the function, before the very last bracket
return(true);
...and you should be good.
bool CandleBelowMA40() { CandleInfo(); Ema40(); for(i = ArraySize(Open)-1; i >= 0; i--) { if(Open[i]>Close[i]) { if(Open[i]>NormalizeDouble(Ema40[i],_Digits)) { return false; break; <<<<<<<<<<<<<<<<<<<<<<<<<<< this is never run. } } else if(Close[i]>Open[i]) { if(Close[i]>NormalizeDouble(Ema40[i],_Digits)) { return false; break; <<<<<<<<<<<<<<<<<<<<<<< this is never run. } } } <<<<<<<<<<<<<<<<<<< what are you returning here? } // CandleBelowMA40
Now, imagine that for whatever reason, when you enter the "CandleBelowMA40" function, i = -1. Maybe you don't expect that to happen, but it might. That means that the "for" loop won't be executed, and you'll get to the end of the function without having a value to return. Also, maybe the flow enters the "for", but what would happen if "Open[i]" is equal to "Close[i]"? You won't enter the "if" nor the "else", am I right? That's what that error means. You have to control all paths, and make sure you ALWAYS return a value if your function is meant to return a value. If you don't expect "i" to be -1, then just put at the end of the function, before the very last bracket
...and you should be good.
ya I should be good haha
bool CandleBelowMA40() { CandleInfo(); Ema40(); for(i = ArraySize(Open)-1; i >= 0; i--) { if(Open[i]>=Close[i]) <<<<<<I add this { if(Open[i]>NormalizeDouble(Ema40[i],_Digits)) { return false; } } else if(Close[i]>=Open[i]) <<<<<<I add this { if(Close[i]>NormalizeDouble(Ema40[i],_Digits)) { return false; } } } return(true); <<<<<<This is what you want me to add? }
Sorry for late reply, I have already sleep yesterday,
so what u told me yesterday is I need to control all path,
so "if" and "else if" condition statement might not be executed,
I still need a return value, and that's why u call me add a return value before the very last bracket,
am I right?
I have another question, for example
bool CandleBelowMA40() { CandleInfo(); Ema40(); for(i = ArraySize(Open)-1; i >= 0; i--) { if(Open[i]>=Close[i]) { if(Open[i]>NormalizeDouble(Ema40[i],_Digits)) { return false; <<<<<<here } } <<<<<the rest of the code from here else if(Close[i]>=Open[i]) { if(Close[i]>NormalizeDouble(Ema40[i],_Digits)) { return false; } } } <<<<<to here }
lets say it run until here, so it return a false,
and the rest of the code will not be read,
am I right?
The function returns a bool.
bool CandleBelowMA40() { CandleInfo(); Ema40(); for(i = ArraySize(Open)-1; i >= 0; i--) { if(Open[i]>=Close[i]) { if(Open[i]>NormalizeDouble(Ema40[i],_Digits)) { return false; } } else if(Close[i]>=Open[i]) { if(Close[i]>NormalizeDouble(Ema40[i],_Digits)) { return false; } } } return(true); }
like this?
lets say it run until here, so it return a false,
and the rest of the code will not be read,
am I right?
Yes, you're right. When there's a "return" the rest is not executed. If you have many nested "if" and "for" you might forget a path, so it's always a good idea to return something "generic" before the end of the function - it might be true or false (if your function returns a boolean), whatever harms the least your flow in case it's executed unexpectedly.
Yes, you're right. When there's a "return" the rest is not executed. If you have many nested "if" and "for" you might forget a path, so it's always a good idea to return something "generic" before the end of the function - it might be true or false (if your function returns a boolean), whatever harms the least your flow in case it's executed unexpectedly.
thanks man.
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
the compiler said the function CandleBelowMa40(),
"not all control paths return a value",
why?