function with array as return value?

 

is it possible to give an array as a return value, like this?


double[] function23(...)
{
   double erg[3];
   ...
   
   return (erg);
} 
 
mql4_rulez:

is it possible to give an array as a return value, like this?



mql4_rulez,

You normally do this by returning a pointer to the array. Since pointers are not supported in mql4 seems impossible to do it that way.

Regards!

 
mql4_rulez wrote >>

is it possible to give an array as a return value, like this?

void function(double& erg[])

 
mql4_rulez wrote >>

is it possible to give an array as a return value, like this?

Like this:

//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start(){
   double array[10];
  
   myFunction(array);
   
   for(int i = 0; i < ArraySize(array); i++){
      Print("  ", i, "  array[i] = ", array[i]);
   }

   return(0);
}


double myFunction( double& theArray[]){
   
   for(int i = 0; i < ArraySize(theArray); i++){
      theArray[i] = i+10;
   }
   return(theArray);
}


2009.05.24 04:04:38 test AUDCHF,H1:   9  array[i] = 19
2009.05.24 04:04:38 test AUDCHF,H1:   8  array[i] = 18
2009.05.24 04:04:38 test AUDCHF,H1:   7  array[i] = 17
2009.05.24 04:04:38 test AUDCHF,H1:   6  array[i] = 16
2009.05.24 04:04:38 test AUDCHF,H1:   5  array[i] = 15
2009.05.24 04:04:38 test AUDCHF,H1:   4  array[i] = 14
2009.05.24 04:04:38 test AUDCHF,H1:   3  array[i] = 13
2009.05.24 04:04:38 test AUDCHF,H1:   2  array[i] = 12
2009.05.24 04:04:38 test AUDCHF,H1:   1  array[i] = 11
2009.05.24 04:04:38 test AUDCHF,H1:   0  array[i] = 10
 

here is my solution:

MT4:

#import "FXdeltaMT4DLL.dll"
     double regression(double x[], double y[], int xdim, int ySize, double params[], double intercept);
#import
nt init(){
   //---- indicators
   SetIndexStyle( 0, DRAW_LINE, 0, 1, Red);
   SetIndexLabel( 0, "Sample Debug" );  
   double x[9];
   double y[9];
   double intercept=1.0;
   int i=0;
   while(i<9){
      x[i]=i;
      i++;
   }
   y[0]=0;
   y[1]=2;
   y[2]=2;
   y[3]=3;
   y[4]=6;
   y[5]=5;
   y[6]=6;
   y[7]=16;
   y[8]=20;
   string s;
   s="Everything OK darlin'.";
   //s=GetStringValue("j");
   Hello(s,x);
   regressionWithInfo(x, y, 1, 9, intercept);
   
   double xx[18];
   i=0;int j=0;
   while(i<9){
      xx[i]=i;
      i++;
   }
   while(i<18){
      xx[i]=j*j;
      i++;j++;
   }
   regressionWithInfo(xx, y, 2, 9, intercept);
   double params[3];//container for output parameters of regression
   regression(xx, y, 2, 9, params, intercept);//compute
   Print("params0:"+params[0]);
   Print("params1:"+params[1]);
   Print("params2:"+params[2]);

C++:

FXDELTADLL_API double * __stdcall FXfunc::CFXdeltaDLL::regression(double *xdbl, double *ydbl, int xdim, int ySize, double *params, double intercept){
        
        std::vector<Array> x(ySize,xdim);
        std::vector<Real> y(ySize);
        int n=0;
        int col=0;
        do{
                col=(int)(n/ySize);
                x[n-col*ySize][col]=*xdbl++;
                n++;
        }while(n<ySize*xdim);

        n=0;
        do{
                y[n]=(*ydbl++);
                n++;
        }while(n<ySize);

        MyQuantlibLinearRegression * lr=new MyQuantlibLinearRegression(x,y,intercept);

        for(int j=0;j<lr->dim();j++){
                params[j]=lr->params(j);
        }
        
        return params;
}
 

Or ... it may not be be neccessary to return the array at all

double array[10] //declare it globaly so no need to return it

int start()
{
  myfunctionarray();
  for(int i = 0; i < ArraySize(array); i++)
   {
    Print("  ", i, "  array[i] = ", array[i]);
   }
  return(0)
}

void myfunctionarray()
(
  ArrayInitialize(myarray,0);
  //do array stuff
  return;
}

 
SDC:

Or ... it may not be be neccessary to return the array at all

That limits the functionality of the Function to a single array.
 

Yes it does, but then I did say it may not be neccessary, not that it is never neccessary ;) Having said that, it wouldnt be difficult to pass parameters to a simalar function to make it work with other global arrays.

 
phy:

Like this:

Thanks phy! Much appreciated..
 
mql4_rulez: is it possible to give an array as a return value, like this?
double[] function23(...)
No but you can do this
double result[]; function23(result...);
:
void function23(double& result[]...){ ArrayResize(result,...); ... }
See Could EA Really Live By Order_History Alone? - MQL4 forum
 
phy:


//+------------------------------------------------------------------+
//| script program start function                                    |
//+------------------------------------------------------------------+
int start(){
   double array[10];
  
   myFunction(array);
   
   for(int i = 0; i < ArraySize(array); i++){
      Print("  ", i, "  array[i] = ", array[i]);
   }

   return(0);
}


double myFunction( double& theArray[]){
   
   for(int i = 0; i < ArraySize(theArray); i++){
      theArray[i] = i+10;
   }
   return(theArray);
}

 


If i use you example I get an error message at the point where the function is supposed to return the Array. That error says "Invalid array access"

 

can you help me with this? 

Reason: