Is it possible to pass 2 one-dimensinal arrays to a function?

 

Just as the questions says, and if so, how?

Or, can only one array be passed to function?

For example, if I needed 2 data sets, I would have to combine them to form a 2-dimensional array...?

Thanks.

 

Yes you can pass any number of any datatype, but the function can return only one parameter.

<https://docs.mql4.com/basis/functions/call>

 

Surprisingly that example says parameters are passed by value but doesn't mention that in the example the array is passed by pointer.

int start(){
   double some_array[4]={0.3, 1.4, 2.5, 3.6};
   double a=linfunc(some_array, 10.5, 8);
   //...
}

double linfunc(double x[], double a, double b){
   return (a*x[0] + b);
}

Passing by pointer is very important as it means nothing much is copied, you just tell the function the address of where to look for the data

 
DayTrader:

... but the function can return only one parameter.

Of course you can pass parameters by reference instead, in which case you can effectively return lots of values by changing the referenced parameters.

https://docs.mql4.com/basis/variables/formal

 
dabbler:

Of course you can pass parameters by reference instead, in which case you can effectively return lots of values by changing the referenced parameters.

https://docs.mql4.com/basis/variables/formal


OK, thanks. Just to clarify, what I am doing is building a function to calculate "r" (linear correlation coefficient) with 2 different data sets (profits vs. MAE or MFE).

I already have 1-dimensional arrays that contain the data for each trade generated in the backtester: one array containing profits, another array containing

MAE (or MFE). I think it would be too cumbersome to combine these into several 2-dimensional arrays to pass to the r calculation function. Instead, I think it

would be much simpler to just pass 2 separate 1-dimension arrays to the function. Here is a code sample:

double rLinCorCeoff(double DataArray[], double MAEMFEArray[]) // function header
{
// code for calculations 
} 


// ---- later in the program

rNetProfMFE=rLinCorCeoff(ProfitLossinDollarsArray,MFEinDollarsArray); // sets variable rNetProfMFE to the value returned by rLinCorCoeff

I don't want to pass the arrays by "&reference" because I don't want to change the values of the arrays; they are being used by other parts of the program for

other calculations.

My question:

Will the above code work?

 
outofdebt:


OK, thanks. Just to clarify, what I am doing is building a function to calculate "r" (linear correlation coefficient) with 2 different data sets (profits vs. MAE or MFE).

I already have 1-dimensional arrays that contain the data for each trade generated in the backtester: one array containing profits, another array containing

MAE (or MFE). I think it would be too cumbersome to combine these into several 2-dimensional arrays to pass to the r calculation function. Instead, I think it

would be much simpler to just pass 2 separate 1-dimension arrays to the function. Here is a code sample:

I don't want to pass the arrays by "&reference" because I don't want to change the values of the arrays; they are being used by other parts of the program for other calculations.

My question:

Will the above code work?

Yes I believe so, and you can't pass arrays by reference anyway.
 
dabbler:
Yes I believe so, and you can't pass arrays by reference anyway.

Cool thanks.
Reason: