What it means
A function declared to return a value (a double, a bool, an int) can reach its closing brace without a return. MetaEditor build 6275 reports it as error 117 on that closing brace, so the program doesn't compile.
Usual causes
- An
if/else ifchain without a finalelse. Every condition can be false, and then nothing is returned. - A
switchwithoutdefaultwhose cases each return. - A loop that returns from inside, with nothing after the loop for the case where it never returns.
How to detect it
- MetaEditor's Errors tab shows
'}' - not all control paths return a valueon the line of the function's closing brace. The missing path is somewhere above it.
Broken code
MetaEditor reports the error on line 8, the function's closing brace: a symbol with 0, 1 or 6 digits falls through both conditions.
double PipSize(const string symbol)
{
int digits = (int)SymbolInfoInteger(symbol, SYMBOL_DIGITS);
if(digits == 3 || digits == 5)
return 10 * SymbolInfoDouble(symbol, SYMBOL_POINT);
else if(digits == 2 || digits == 4)
return SymbolInfoDouble(symbol, SYMBOL_POINT);
}
void OnStart()
{
Print("Pip size: ", DoubleToString(PipSize(_Symbol), 8));
}Fixed code
The same program, corrected. It compiles in MetaEditor with 0 errors and 0 warnings.
double PipSize(const string symbol)
{
int digits = (int)SymbolInfoInteger(symbol, SYMBOL_DIGITS);
double point = SymbolInfoDouble(symbol, SYMBOL_POINT);
if(digits == 3 || digits == 5)
return 10 * point;
// Every other case: one point is one pip
return point;
}
void OnStart()
{
Print("Pip size: ", DoubleToString(PipSize(_Symbol), 8));
}Why the fix works
The function now ends with an unconditional return, so every path through it returns a value. Choosing what that last value is matters as much as adding it: here, a symbol with an unusual number of digits uses one point as its pip, instead of an undefined result.
Questions
Is "not all control paths return a value" an error or a warning in MQL5?
MetaQuotes' documentation lists it both among compiler warnings and among errors; MetaEditor build 6275 reports it as error 117, so the program doesn't compile until every path returns.
Can I just add return 0 at the end?
It compiles, but think about what 0 means to the caller. A pip size, a lot size or a price of 0 can cause a zero divide or a refused order later. Return a value that makes sense, or one the caller checks for.
The programs on this page are minimal examples written to show the error and its fix, not trading systems. Some of them send orders when they run: try them on a demo account. Trading involves substantial risk of loss and is not suitable for every investor. Edgoro is a software tool, not a broker or financial adviser: nothing it produces is investment advice, and past or backtested performance does not guarantee future results.