Can Arrays be passed and returned as parameters in custom functions?

 

Can Arrays be passed and returned as parameters in custom functions?


I try this and it doesn't work:


string someArray = returnArray();

OR

string someArray;

ArrayCopy( someArray, returnArray() );



string returnArray()

{

string a[3];


a[0] = "A";

a[1] = "B";

a[2] = "C";


return(a);

}


Any tips?

 

you have to pass the by reference...


string a[3];

returnArray(a);
print(a[0]);

void returnArray(string& a[]){


a[0] = "A";
a[1] = "B";
a[2] = "C";

}
 
Russell:

you have to pass the by reference...


Hey thanks, I didn't know you could referance variables. Do you know where I can find any documentation on that? Does the "&" statement mean to referance? Aslo, the a[] parameter, I thought you always had to put a size in the array declaration (a[3]) or else it wouldn't work. What does a[] mean? Blaaaa

 

probably in the book somewhere https://book.mql4.com//


Yes the & sign is many languages used for reference params.

In general that is true. But since you are passing by reference the the var is already be decleared somewhere. in this case "string a[3]" To make the example more explainatory


string gsText[3];

returnArray(gsText,0);
print(gsText[1]);// prints B
gsText[1] = "foo";
returnArray(gsText,1);
print(gsText[1]);// prints foo

void returnArray(string& rsText[], int iRun){

  if (iRun == 0){
    rsText[0] = "A";
    rsText[1] = "B";
    rsText[2] = "C";
  }

}


gsText

g = global

s = string


rsText

r = return


so rsText is really a pointer to gsText. But a C programmer can explain this stuff far better I guess



hth

 
https://docs.mql4.com/basis/variables/formal
 
Thanks, that makes total sense now. Dam, I can't believe I missed that in the documentation, I've been through it a trillion times. Also, I couldn't figure out how to pass an array (not by reference, but by value) to functions, that page and your comment answered both. I new there had to be a way. Thanks again.