|
Parameters passed to the function are local. Scope is the function block.
Formal parameters must have names differing from those of external variables and local variables defined within one
function. In the block of the function to the formal parameters some values can be assigned. Some values can be assigned to
formal parameters in the function block.
Example:
void func(int x[], double y, bool z)
{
if(y>0.0 && !z)
Print(x[0]);
...
}
Formal parameters can be initialized by constants. In this case, the initializing value is considered as the default
value. Parameters, next to the intialized one, must also be initialized.
Example:
void func(int x, double y = 0.0, bool z = true)
{
...
}
When calling such a function, the initialized parameters can be omitted, the defaults being substituted instead of them.
Example: func(123, 0.5);
MQL4-library functions imported within other modules cannot have parameters initialized by default values.
Parameters are passed by value, i.e., modifications of the corresponding local variable inside the called function
will not be shown in the calling function in any way. It is possible to pass arrays as parameters. However, for an array passed as parameter,
it is impossible to change values of its elements.
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.
Into function cannot be passed more than 64 parameters.
|