It seems the compiler only passes the reference when the argument is an array; any other time, it takes the value of the argument, not the reference. It treats the variable as a local variable to the function; that's why you don't see the change taking effect in the global scope.
For instance:
void foo(double& x[]) //reference to array x is passed!
void foo(double& x) //apparently x is passed by value, no reference passed!
Why this happens? I don't know. It's not well documented in the editor's help. Maybe someone from Metaquotes could clarify this better.
P.S. Also, in the second example, notice how the compiler accepts the statement as a 'legal' statement, even though it gives one type of argument a totally different interpretation than the other. This semantic ambiguity could be dangerous, allowing for the introduction of logic errors in the code which could be hard to detect.
It seems the compiler only passes the reference when the argument is an array; any other time, it takes the value of the argument, not the reference. It treats the variable as a local variable to the function; that's why you don't see the change taking effect in the global scope.
For instance:
void foo(double& x[]) //reference to array x is passed!
void foo(double& x) //apparently x is passed by value, no reference passed!
Why this happens? I don't know. It's not well documented in the editor's help. Maybe someone from Metaquotes could clarify this better.
P.S. Also, in the second example, notice how the compiler accepts the statement as a 'legal' statement, even though it gives one type of argument a totally different interpretation than the other. This semantic ambiguity could be dangerous, allowing for the introduction of logic errors in the code which could be hard to detect.
You are right! It only works for arrays and nothing else! MQL4 is certainly a strange language.
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
I tried to use the & to return values in a library and it does not seems to work, but in the program main it is fine
eg if this function is in the program main, it works
void CalValue(string symbol, int magic, double& value1, double& value2, double& value3 )
but if I place this function in a library, values are always zero
any idea to this? thks