not all control paths return a value

 

Good morning, the editor gives me the following error: “not all control paths return a value”.

Do I need to place return(0) at the end of every “for” “if” and all other functions?

For instance, where do I place return(0) in the code below?

Thank you!

for(cnt=0;cnt<=total;cnt++)
     {
     if ( OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))
        { 
        if (OrderSymbol()==Symbol()  &&  GlobalVariableGet("Close_Positions")==1  ) 
           { 
              if (OrderType()==OP_BUY)
                 { 
                 OrderClose(OrderTicket(),OrderLots(),Bid,NULL,PaleGoldenrod);       
                 Print("Error closing BUY order @ session closing: ",GetLastError()); 
                 }       
              if (OrderType()==OP_SELL)
                 { 
                 OrderClose(OrderTicket(),OrderLots(),Ask,NULL,PaleGoldenrod);      
                 Print("Error closing SELL order @ session closing: ",GetLastError());    
                 }      
           }
        }   
     }       
 
Alberto Tortella:

Good morning, the editor gives me the following error: “not all control paths return a value”.

Do I need to place return(0) at the end of every “for” “if” and all other functions?

The expert has many of them so I would prefer to be sure before to proceed.

Thank you!

int function() return(int);
bool function() return(bool);
double function() return(double);
string function() return(string);
!!!!!
void function() return; //<--- nothing
 

At the end of every function in this way?

for(cnt=0;cnt<=total;cnt++)
     {
     if ( OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))
        { 
        if (OrderSymbol()==Symbol()  &&  GlobalVariableGet("Close_Positions")==1  ) 
           { 
              if (OrderType()==OP_BUY)
                 { 
                 OrderClose(OrderTicket(),OrderLots(),Bid,NULL,PaleGoldenrod);       
                 Print("Error closing BUY order @ session closing: ",GetLastError()); 
                 return(0);
                 }       
              if (OrderType()==OP_SELL)
                 { 
                 OrderClose(OrderTicket(),OrderLots(),Ask,NULL,PaleGoldenrod);      
                 Print("Error closing SELL order @ session closing: ",GetLastError());  
                 return(0);
                 } 
            return(0);     
           }
        return(0);
        } 
     return(0);  
     }      
 
Alberto Tortella:

At the end of every function in this way?

No. These are loops. Watch out for the function header, it's higher in your code. 

void MyFunction() <---- NO RETURN VALUE
{
for(cnt=0;cnt<=total;cnt++)
     {
     if ( OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))
        { 
        if (OrderSymbol()==Symbol()  &&  GlobalVariableGet("Close_Positions")==1  ) 
           { 
              if (OrderType()==OP_BUY)
                 { 
                 OrderClose(OrderTicket(),OrderLots(),Bid,NULL,PaleGoldenrod);       
                 Print("Error closing BUY order @ session closing: ",GetLastError()); 
                 }       
              if (OrderType()==OP_SELL)
                 { 
                 OrderClose(OrderTicket(),OrderLots(),Ask,NULL,PaleGoldenrod);      
                 Print("Error closing SELL order @ session closing: ",GetLastError());  
                 }  
           }
        } 
     }      
}
bool MyFunction()
{
for(cnt=0;cnt<=total;cnt++)
     {
     if ( OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))
        { 
        if (OrderSymbol()==Symbol()  &&  GlobalVariableGet("Close_Positions")==1  ) 
           { 
              if (OrderType()==OP_BUY)
                 { 
                 OrderClose(OrderTicket(),OrderLots(),Bid,NULL,PaleGoldenrod);       
                 Print("Error closing BUY order @ session closing: ",GetLastError()); 
                 }       
              if (OrderType()==OP_SELL)
                 { 
                 OrderClose(OrderTicket(),OrderLots(),Ask,NULL,PaleGoldenrod);      
                 Print("Error closing SELL order @ session closing: ",GetLastError());    
                 }      
           }
        }   
     }  
return(true) <----- RETURN VALUE BOOLEAN
}

.... etc 

 
  1. Alberto Tortella: Do I need to place return(0) at the end of every “for” “if” and all other functions?

    You need to return a value wherever you exit the function. Like at the closing brace.

  2. GlobalVariableGet("Close_Positions")==1
    Terminal variables are for inter-process communication (e.g. EA to EA) and as a poor man's persistent storage. That makes your code incompatible with itself on other charts.

  3.                  OrderClose(OrderTicket(),OrderLots(),Bid,NULL,PaleGoldenrod);       
                     Print("Error closing BUY order @ session closing: ",GetLastError()); 
                     return(0);
    Why are you returning after one? Why not complete the loop in case you have more orders open and return at the end.
  4. Use OrderClosePrice and you don't have to differentiate Bid/Ask.
  5. Don't look at last error unless you have an error. Check your return codes for errors, report them and you would know why. Don't just silence the compiler, it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    Only those functions that return a value (e.g. iClose, MarketInfo, etc.) must you call ResetLastError before in order to check after.

  6. for(cnt=0;cnt<=total;cnt++)
    If there are total orders, their positions are [0 ... total -1] Drop the equals.
  7. In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing:
    1. For non-FIFO (US brokers,) (or the EA only opens one order per symbol,) you can simply count down in a position loop, and you won't miss orders. Get in the habit of always counting down.
                Loops and Closing or Deleting Orders - MQL4 and MetaTrader 4 - MQL4 programming forum
      For FIFO (US brokers,) and you (potentially) process multiple orders per symbol, you must count up and on a successful operation, reprocess all positions (set index to -1 before continuing.)
    2. and check OrderSelect in case earlier positions were deleted.
                What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
                Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    3. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use the Predefined Variables (Bid/Ask) or OrderClosePrice() instead, on the next order/server call.