'Return' ing arrays?

 
Hi,

I've been playing around with functions and various ways of returning variables in MQL 4. One thing I do a lot of in several other programming languages I use is return arrays to the calling code. Is it possible to return arrays or only single variables? I presume that the returning of arrays doesn't work as I haven't had much success - could somebody confirm this for me?
 
no problem. corresponding article coming soon. see example
int start()
  {
    double SomeArray[5];

    SomeArray[1]=1;
    SomeArray[2]=2;
    SomeArray[3]=3;
    SomeArray[4]=4;
    SomeArray[0]=SomeArray[4]*2;

    int cnt=Subroutine2(SomeArray);
    for(int i=0; i<cnt; i++)
        Print(i, " - ", SomeArray[i]);

    return(0);
  }
int Subroutine2(double& par3[])
   {
    int i,cnt=ArraySize(par3);
    for (i=0; i<cnt; i++)
      {
        par3[i]*=2;
      }
    return(cnt);
  }
 
Hi Slawa,

Thanks for your response - however, maybe I wasn't clear enough? What I want to do is actually return an Array to the calling code - i.e. do a 'return(someArray)'. I see that in the example above, you're calling 'Subroutine2' with an Array as one of it's parameters but returning a single variable (cnt) - it's an Array I would like to return.

I've got around this for the moment by declaring an Array outside of all functions, which any function can use. A called function would do what it has to do, fill this array with the results and 'return(0)' to the calling code. The calling code would then grab the results in this array and process them as required..... It's a bit messy but it works.
 
Cohen
(double& par3[]) means transferring parameter by reference. therefore all changes are reflected in the initial array. You can pass parameters of any type by reference and not only arrays.
of course declaring array outside all functions is legal solution too.
 
Aaah - so '&' can be used for referencing - I was wondering if this was possible somehow. Thanx!
 
Hi,

With regards to referencing.... I've tried to compile the following code but get the error "'&' - unexpected token" on the line in the try() function containing the return (see below):

int start() {
double numbers[] = try();
Print("num0: " + numbers[0] + "num1: " + numbers[1]);
}

double try() {

// These results would normally contain the results of the function...
double results[4];
results[0] = 0.4;
results[1] = 0.7;
results[2] = 0.9;
results[3] = 0.42;

return (&results);
}

Is it only possible to pass references through function parameters or should the above be legal code?
 
no. that way is impossible. only one simple value can be returned because we have not address arithmetic
 
Thanx again Slava!
Reason: