Passing ParametersThere are two methods, by which the machine language can pass arguments to a subprogram (function). The first method is to send a parameter by value. This method copies the argument value into a formal function parameter. Therefore, any changes in this parameter within the function have no influence on the corresponding call argument.
The second method is to pass by reference. In this case, reference to a parameter (not its value) is passed to a function parameter. Inside the function, it is used to refer to the actual parameter specified in the call. This means that the parameter changes will affect the argument used to call the function.
MQL4 uses both methods, with one exception: arrays, structure type variables and class objects are always passed by reference. In order to avoid changes in actual parameters (arguments passed at function call) use the access specifier const. When trying to change the contents of a variable declared with the const specifier, the compiler will generate an error. NoteIt should be noted that parameters are passed to a function in reversed order, i.e., first the last parameter is calculated and passed, and then the last but one, etc. The last calculated and passed parameter is the one that stands first after opening parenthesis. Example:
In first call (see example above) the i variable is first used in strings concatenation:
Here its value doesn't change. Then the i variable is used in calculation of the a[i++] array element, i.e. when array element with index i is accessed, the i variable is incremented. And only after that the first parameter with changed value of i variable is calculated. In the second call the same value of i (calculated on the first phase of function calling) is used when calculating all three parameters. Only after the first parameters is calculated the i variable is changed again. See also Visibility Scope and Lifetime of Variables, Overload, Virtual Functions, Polymorphism |