Using the while operator to return type void within void ... haha

 

This might be a dumb question, if yes - I apologize for my lack of know how.

I am curious if this is okay code. I'm talking about the while operator at the end where I call the ModifyShorts() again until the result is false.

I'm just trying to make sure I'm bug free and that the code completes it's assignment.

MetaEditor help states that the return operator should not contain an assignment operator, but void is not an operator.


void ModifyShorts()
{
  int totalorders = OrdersTotal();
  for(int i=totalorders-1;i>=0;i--)
 {
    OrderSelect(i, SELECT_BY_POS);
    bool result = false;
    if ((OrderSymbol() == Symbol()) && (OrderMagicNumber() == ExpertNumber && (OrderType() == OP_SELL )))

     {result = OrderModify(OrderTicket(), OrderOpenPrice(),ObjectGet ("xTBUpper", OBJPROP_PRICE1),ObjectGet ("xTBLower", OBJPROP_PRICE1),0, CLR_NONE); RefreshRates();}   
 
  
    } 

while (result>0)     
    {ModifyShorts();}

}
 

Recursive calls don't work well with the tester so I would avoid such code. Better do it iteratively.

 

maybe i get it wrong. but this should cause a endless loop. the "result" in upper iterations are not modified in a lower level of rekursion. since modify shorts does not return anything and result itself is not modified inside the while loop. Maybe works in mql since i have heard it doesn't use a namespace for variables. But if it works, its not nice to read, nor economic to run.

 

You can easily shoot yourself into the foot when using recursions, especially when the same code can (and should) be written without recursion. Recursion is usually only¹ used when the problem itself is recursive by its nature, for example iterating over all nodes in a tree (for example all folders and sub-folders and sub-sub-folders, etc). Recursion will heavily use the stack because each function call needs to return eventually and so it must remember each recursive function call to be able to unwind it and return from each and every one of them again at the end if it is done. This can easily lead to a stack overflow if it is not known beforehand or somehow limited how many levels deep it might go.

Repeatedly trying to do the same thing until it succeeds is not a recursive problem at all and you should do it in a loop that repeatedly calls the function from the outside and not let the function call itself.

Your above code is the wrong solution to the problem!


_____
¹) There are languages like LISP for example that do even "normal" loops as recursions but these languages can avoid using the stack for remembering each function call when the recursion is written in such a way that between recursive call and return nothing has to happen anymore and all recursive returns could be done in one in one single step.

 

Thanks guys for you expertise!

It did not go unappreciated! :)

Reason: