NavigationUser loginWho's new
Who's onlineThere are currently 0 users and 30 guests online.
|
Lesson 8 - Variables in MQL4What are the variables mean?
As I told you the secret before, the variables are the names that refer to sections of memory into which data can be stored. To help you think of this as a picture, imagine that memory is a series of different size boxes. The box size is memory storage area required in bytes.
When we create a variable we are telling the computer that we want him to assign a specified memory length (in bytes) to our variable, since storing a simple number, a letter or a large number is not going to occupy the same space in memory, so the computer will ask us what’s the kind of data and how much the length of the data? That is the Data type for.
For example if we said this line of code to the computer:
That’s mean we are asking the computer to set a block of 4 bytes length to our variable named “MyVaraiable”.
In the previous example we have used: int ß Keyword int ß Integer data type. int ß Declaration MyVaraible ß Variable’s constant. =0 ß Initialization
We will know more about variables in a coming lesson.
In MQL4, these are the kinds of Data types:
I’ve copied the previous few lines from the DATA TYPES lesson for you. To know what’s the variable, now how do to declare the variables: Declaration: Declaring a variable means to introduce it to the world and specify its type. By using the keywords you have learned in the DATA TYPES lesson (int, double, char, bool, string, color and datetime) with the name you chose to the variable. For example:
Here you declared a variable named MyVaraible which is an integer type. And before the declaration you can’t use the MyVariable in your code. If you used it without declaration the MQL4 compiler will complain and will tell you something like this:'MyVaraible' - variable not defined. 1 error(s), 0 warning(s). Initialization: Initializing the variable means to assign a value to it, for example MyVaraible=0; You can initialize the variable at the same line of the declaration like the example: int MyVaraible=0; And you can declare the variable in one place and initialize it in another place like this:
But keep in your mind this fact: the declaration must be before the initialization. Scopes of variables: There are two scopes of the variables, Local and Global. Scope means, which part of code will know about the variable and can use it. Local variable means they are not seen to outside world where they had declared. For example the variables declared inside function are local to the function block of code, and the variables declared inside the loop or decisions block of code are local to those blocks and can be seen or used outside them. For example:
In the above example the variables a,b,c and d are local variables, which can be used only inside the function block of code ( any thing beside the braces) and can’t be used by outside code. So we can’t write a line after the function above saying for example: d=10; because d is not seen to the next line of the function because it’s outside it. The second kind of the scopes is the Global variables, and they are the variables which had declared outside any block of code and can be seen from any part of your code. For example:
Here the variable Global_Variable declared outside the function (function level declaration) so, it can be seen by all the functions in you program. The Global variables will automatically set to zero if you didn’t initialize them. Extern variables: The keyword “extern” used to declare a special kind of variables; those kinds of variables are used to define input date of the program, which you can set them form the property of your Expert advisor or Custom indicator. For example:
Here the variable Indicator_color had defined as an extern variable which you will see it the first time you attach your indicator (or EA) to the MetaTrader chart and which you can change it from the properties sheet windows. Look at Figure 1.
Figure 1: Property sheet of MA indicator Here the variables Period, Shift, MA_method, Apply_to and Style are variables defined using the “extern” keyword so they appear in the property sheet. Any variable you want the user of your program be able to change and set, make it extern variable. |