1. No, there is not another way.
2. See https://docs.mql4.com/basis/variables/formal:
It is also possible to pass parameters by reference. In this case, modification of such parameters will be shown in the corresponding variables in the called function passed by reference. Array elements cannot be passed by reference. Parameters can be passed by reference only within one module, such possibility being not provided for libraries. In order to inform that a parameter is passed by reference, it is necessary to put the & modifier after the data type.
Example:
void func(int& x, double& y, double& z[])
{
double calculated_tp;
...
for(int i=0; i<OrdersTotal(); i++)
{
if(i==ArraySize(z)) break;
if(OrderSelect(i)==false) break;
z[i]=OrderOpenPrice();
}
x=i;
y=calculated_tp;
}
Arrays can be passed by reference, as well, all changes being shown in the source array. Unlike simple parameters, arrays can be passed by reference into library functions, as well.
Parameters passed by reference cannot be initialized with defaults.
Passing parameters as references can help too. Thanks much.
I hope you understand me.
Rosh thank you, but maybe I wasn't so clear: I want to retrun an array from a function (not to get a array into the function).
The array has been passed by reference, which means that any changes done on z[] inside the function, is done on the array passed to that function, hence the array is "returned".
The array has been passed by reference, which means that any changes done on z[] inside the function, is done on the array passed to that function, hence the array is "returned".
Just to confirm and reinforce my understanding, passing by reference has an effect similar to a static variable in that you can change the value of the parameter within a function where the variable(parameter) is normally outside the scope of the function being called?
So if I may construct a crude example :=
void No_Name_Function() { int X = 1; Function_Change_X(X); ..................X now equals 2??? } void Function_Change_X(int& New_X) { New_X ++; }
Just to confirm and reinforce my understanding, passing by reference has an effect similar to a static variable in that you can change the value of the parameter within a function where the variable(parameter) is normally outside the scope of the function being called?
No, static variables have nothing to do with this... Maybe u meant global variables (https://docs.mql4.com/basis/variables/global), but the last part of your statement ("...the variable(parameter) is normally outside the scope of the function
being called?") is inaccurate in regard to global variables since the scope of a global variable is the entire program (including all functions). So I don't know what u meant.
Without getting into technical details about what is actually being passed, here's a simplified explanation of the differences between pass-by-value and pass-by-reference:
- When passing a parameter by value to a function, the function receives a copy of the parameter.
- When passing a parameter by reference to a function, the function receives the actual parameter.
p.s. Your example is fine.
Ah, ok, my bad. Thanks for clearing that up for me.
Good all, Please I have the need to include yearly period in my indicator. How can I do this?
example:
if(Period()<=60) period = PERIOD_D1;
if(Period()==240) period = PERIOD_W1;
if(Period()>=1440) period = PERIOD_MN1;
if(Period()>=43200) period = PERIOD_??;
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi
1. In MQ4, I understand that there is no concept of classes and structs. The only way I found for holding lot of data is arrays (buffer) or save to a file. Is there any other way?
2. It doesn’t say anywhere but I wasn’t able to return an array from a function. Is it the right behavior?
Thanks
Pankaj