packy
packy

Reputation: 71

How to return an array from a function in MQL4?

I would like to return an array from my function, how can I do that?
Look!

int GetOrdresVente(){
    int ordrevente;
    int Tabordresvente[];
    for(int j = OrdersTotal() - 1; j >= 0 ; j--){
        if(OrderSelect( j, SELECT_BY_POS ) == true){
            if(OrderSymbol() == Symbol()){
                if(OrderType() == OP_SELL ){
                    ordrevente = OrderTicket();
                    ArrayResize( Tabordresvente, ArraySize( Tabordresvente ) + 1);
                    Tabordresvente[ArrayResize( Tabordresvente, ArraySize( Tabordresvente ) - 1 )] = ordrevente;
                }
            }
        }
    }
    return Tabordresvente;
}

Thanks for replies!

Upvotes: 7

Views: 9772

Answers (1)

Daniel Kniaz
Daniel Kniaz

Reputation: 4681

Not possible. Create an array, pass it into the function, run inside the function as arrays are always passed by reference and never by value.

void OnTick(){
   int array[];
   GetOrdresVente(array);
}

void GetOrdresVente(int &array[]){
   //use counter and size of the array or CArrayInt* and do not resize every time
   int counter=0,size=OrdersTotal();
   ArrayResize(array,size);
   for(int i=OrdersTotal()-1;i>=0;i--){
       if(OrderSelect(i,SELECT_BY_POS){
          if(OrderType()==OP_SELL){
             array[counter++]=OrderTicket();
          }
       }
   }
   ArrayResize(array,counter);
   return;
}

Upvotes: 14

Related Questions