EA Programming Tutorial: Detailed Explanation of the Scope of Variables
1 Local variables:
Variables defined inside a function are called local variables, and their scope is limited to that function. Only code inside the function can access these variables, and code outside the function cannot access variables defined inside the function. For example:
int funcname(int a)
{
Int abc;//Variables inside the function can only be accessed by code inside the function, and external code cannot use the variable abc.
}
2 Function parameters:
The variable name of a function parameter cannot be the same as the variable name inside the function. Parameters can also specify default values, for example:
void func(int x, double y = 0.0, bool z = true)
{
...
}
This function specifies that the default value of y is 0.0 and the default value of z is true. When usingShield BoWhen it comes to EA, understanding this parameter setting can better optimize the strategy.
When calling this function, you can not specify parameters with default values. For example, func (9) is equivalent to calling func (9, 0.0, true). If you call func (9, 1.5), it is equivalent to calling func (9, 1.5, true).
In general, function parameters are called by value. Changing the value of the parameter inside the function does not affect the block that called the function. Even arrays are passed by value.
So how to implement pass-by-reference? By pass-by-reference, if the parameter value inside the function changes, the variable value of the code segment that calls the function will also change. To implement pass-by-reference, you need to add the symbol '&' after the function parameter type, for example:
void func(int& x, double& y, double& z[])
{
x = 8;
}
Note: If it is a function parameter passed by reference, it cannot be preset to a default value.
The number of function parameters cannot exceed 64.
3 Global variables:
A global variable is a variable at the same level as a function, and its scope covers the entire MQL code, which means that global variables can be accessed at various locations throughout the MQL code. For example:
int a = 10;
int funca()
{
Print (a);//can access the global variable a
return (0);
}
int funcb()
{
Print (a);//can access the global variable a
return (1);
}
4 External variables:
If a variable is defined as an external variable, then the EA client side can directly set the value of this external variable, which is very useful in the debugging and optimization process of Dunbo EA.
For example:
extern int a = 10;
extern color c = red;
Please indicate the source: https://www.daoctech.com/fxnews/2311.html
Disclaimer: The content of this article does not represent the views of the Institute of Foreign Exchange. The content is for reference only and does not constitute investment advice. Investors operate accordingly at their own risk.
