Question: how to return a double Array from a fuction to a new double Array

 

Hi guys,

thanks for actively responding my first question, much appreciated.

Here is my second question of the my first day.

I have a function called "double removeDuplicatedOrder()" return a double Array, which contains a list of pending order price in double. The "removeDuplicatedOrder()" will be called and the return value from assinged to another double Array(called newPending_Order) in a function called "void processOrderList()". My problem when the "newPending_Order" gets retrun value from "double removeDuplicatedOrder()", i do a print function, which gives me a "all ZERO" empty list.

i have use "=" and ArrayCopy() to get the value and assinged to "newPending_Order", neither is working well. please help. Thanks in advance.

the follow is my code example:

//global double Array variables  for storing pending orders
double Existing_orders[30]; 
double Pending_Order[30];
double NewPending_Order[30];

double removeDuplicatedOrder() {
   for(inti=Arraysize(Existing_orders); i>0; i--) {
     //do something... ...
   }
   return(Pending_Order);
}

void processOrderList(double Existing_orders[]) {
   newPending_Order = double removeDuplicatedOrder();
   print ("my new pending order list: |" + loopthroughArray(NewPending_Order)); // this print function give me A list of thirty ZEROs.

   //Alternatively i try to use ArrayCopy

   newPending_Order = double removeDuplicatedOrder();
   ArrayCopy(NewPending_Order, removeDuplicatedOrder();
   print ("my new pending order list: |" + loopthroughArray(NewPending_Order)); // this print function give me A list of thirty ZEROs.
}
 
wenfeiyunyun:

Hi guys,

thanks for actively responding my first question, much appreciated.

Here is my second question of the my first day.

I have a function called "double removeDuplicatedOrder()" return a double Array, which contains a list of pending order price in double. The "removeDuplicatedOrder()" will be called and the return value from assinged to another double Array(called newPending_Order) in a function called "void processOrderList()". My problem when the "newPending_Order" gets retrun value from "double removeDuplicatedOrder()", i do a print function, which gives me a "all ZERO" empty list.

i have use "=" and ArrayCopy() to get the value and assinged to "newPending_Order", neither is working well. please help. Thanks in advance.

the follow is my code example:

You can't return an array from a function, instead pass the array by reference . . .

https://www.mql5.com/en/forum/139964 or https://www.mql5.com/en/forum/117484 and others, use Google . . .

 
RaptorUK:

You can't return an array from a function, instead pass the array by reference . . .

https://www.mql5.com/en/forum/139964 or https://www.mql5.com/en/forum/117484 and others, use Google . . .

thanks RaptorUK, "instead pass the array by reference", are you refering to create a global variable - global Double Array???
 
It doesn't matter if the arrays passed by ref were global or local defined.
But you can access global arrays without passing them by reference as well.
 
wenfeiyunyun:
thanks RaptorUK, "instead pass the array by reference", are you refering to create a global variable - global Double Array???
i have tried the sample code from https://www.mql5.com/en/forum/139964. i have refactored my code much simple and generic now. thanks very much.
 
gooly:
It doesn't matter if the arrays passed by ref were global or local defined.
But you can access global arrays without passing them by reference as well.
Passing arrays by reference means you can make functions more flexible, they can be tasked to work on any array you pass to them instead of the one globally declared array hard coded into them.
 

This is work

//+------------------------------------------------------------------+
//|                                             sectionPinbar_v1.mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""

extern int numSR = 10;
extern int timeFrame = PERIOD_D1;


//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
      double SR[];
      ArrayResize(SR, numSR);
      getArraySR (SR, timeFrame);
      for(int i = 0; i < ArraySize(SR); i++)
      {
         Print (SR[i]); 
      }
   return(0);
}
//+------------------------------------------------------------------+

void getArraySR (double& SR[], int timeFrame)
{
   for(int i = 0; i < ArraySize(SR); i++)
   {
      SR[i] = i; 
   }
   return (SR);
}


2013.12.20 18:54:17     2009.01.02 11:12  sectionPinbar_v1 EURUSD,M1: 9
2013.12.20 18:54:17     2009.01.02 11:12  sectionPinbar_v1 EURUSD,M1: 8
2013.12.20 18:54:17     2009.01.02 11:12  sectionPinbar_v1 EURUSD,M1: 7
2013.12.20 18:54:17     2009.01.02 11:12  sectionPinbar_v1 EURUSD,M1: 6
2013.12.20 18:54:17     2009.01.02 11:12  sectionPinbar_v1 EURUSD,M1: 5
2013.12.20 18:54:17     2009.01.02 11:12  sectionPinbar_v1 EURUSD,M1: 4
2013.12.20 18:54:17     2009.01.02 11:12  sectionPinbar_v1 EURUSD,M1: 3
2013.12.20 18:54:17     2009.01.02 11:12  sectionPinbar_v1 EURUSD,M1: 2
2013.12.20 18:54:17     2009.01.02 11:12  sectionPinbar_v1 EURUSD,M1: 1
2013.12.20 18:54:17     2009.01.02 11:12  sectionPinbar_v1 EURUSD,M1: 0
Reason: