Lesson 8 - Variables in MQL4

What 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.

  • In order to use a box to store data, the box must be given a name; this process is known as declaration.
  • In the declaration process you use a word tell the computer what’s the kind and size of the box  you want to use, this word known as keyword.
  • It helps if you give a box a meaningful name that relates to the type of information which make it easier to find the data, this name is the variable constant.
  • Data is placed into a box by assigning the data to the box.
  • When we set the value of the box you have created in the same line you declared the variable; this process is known as initialization.

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:

 

int  MyVaraible=0;

 

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:

  • Integer (int)
  • Boolean (bool)
  • Character (char)
  • String (string)
  • Floating-point number (double)
  • Color (color)
  • Datetime (datetime)

 

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:

int  MyVaraible;

 

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:

int  MyVaraible;

MyVaraible=5;

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:

double my_func (double a, double b, double c)

  { 

   int d ;                              

   return (a*b + c);

  }

 

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:

int Global_Variable;

double my_func (double a, double b, double c)

  { 

      return (a*b + c + Global_Variable);

  }

 

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:

extern color Indicator_color = C'0x00,0x00,0xFF'; // blue

int init()

  {

   ...

  }

 

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.