Examples of Implementation
In the previous section we analyzed an example
of special functions execution in a simple Expert Advisor
simple.mq4. For better practice let's analyze some more modifications of this program.
 |
Example of a correct program structure |
As a rule, function descriptions are indicated in the same sequence as they are called for execution by the client terminal, namely first goes the description of the special function init(), then start() and the last one is deinit(). However, special functions are called for execution by the client terminal in accordance with their own properties, that is why the location of a description in a program does not matter. Let us change the order of descriptions and see the result (Expert Advisor possible.mq4).
int Count=0;
int start()
{
double Price = Bid;
Count++;
Alert("New tick ",Count," Price = ",Price);
return;
}
int init()
{
Alert ("Function init() triggered at start");
return;
}
int deinit()
{
Alert ("Function deinit() triggered at exit");
return;
}
Starting this Expert Advisor you will see that the execution sequence of special functions in a program does not depend on the order
of descriptions in a program. You may change the positions of function descriptions in a a source code and the result will be the same as in the execution of the Expert Advisor simple.mq4.
 |
Example of incorrect program structure |
But the program will behave in a different way if we change the head part position. In our example we will indicate start() earlier than the head part (Expert Advisri incorrect.mq4):
int start()
{
double Price = Bid;
Count++;
Alert ("New tick ",Count," Price = ",Price);
return;
}
int Count=0;
int init()
{
Alert ("Function init() triggered at start");
return;
}
int deinit()
{
Alert ("Function deinit() triggered at exit");
return;
}
When trying to compile this Expert Advisor, MetaEditor will show an error message:

Fig. 36. Error message during
incorrect.mq4 program compilation.
In this case the line
int Count=0;
is written outside all functions, but is not at the very beginning of a program, but somewhere in the middle of it.
The defining moment in the program structure is that the declaration of the global variable Count is done after function declaring (in our case - special function start()). In this section we will not discuss details of using global variables; types of variables and usage rules are described in the section Variables. It should be noted here that any global variable must be declared earlier (earlier in text) than the first call to it (in our case it is in the function start()). In the analyzed program this rule was violated and the compiler displayed an error message.
 |
Example of using a custom function |
Now let's see how the program behaves in relation to custom functions. For this purpose let's upgrade the code described in the example
of a simple Expert Advisor simple.mq4 and then analyze it. A program with a custom function will look like this (Expert Advisor userfunction.mq4):
int Count=0;
int init()
{
Alert ("Function init() triggered at start");
return;
}
int start()
{
double Price = Bid;
My_Function();
Alert("New tick ",Count," Price = ",Price);
return;
}
int deinit()
{
Alert ("Function deinit() triggered at exit");
return;
}
int My_Function()
{
Count++;
}
First of all let's see what has chaned and what has remained unchanged.
Unchanged parts:
1. The head part is unchanged.
int Count=0;
2. Special function init() is unchanged.
int init()
{
Alert ("Function init() triggered at start");
return;
}
3. Special function deinit() is unchanged.
int deinit()
{
Alert("Function deinit() triggered at exit");
return;
}
Changes:
1. Added: custom function My_Function() .
int My_Function()
{
Count++;
}
2. The code of the special function start() has also changed: now it contains the custom function call, but there is no Count variable calculation line now.
int start()
{
double Price = Bid;
My_Function();
Alert("New tick ",Count," Price = ",Price);
return;
}
In the section Program Execution we analyzed the order of init() and deinit() execution. In this example these functions will be executed the same way, so we will not dwell on their operation. Let's analyze the execution of the special function start() and the custom function My_Function(). The custom function description is located outside all special functions as it must be. The custom function call is indicated in start() code, which is also correct.
After init() is executed, the program will be executed so:
31.The special function start() is waiting to be started by the client terminal. When a new tick comes, the terminal will start this function for execution. As a result the following actions will be performed:
32 (1). In the line
double Price = Bid;
the same actions are performed:
32.1(1). Local variable Price is initialized (see Types of Variables). Value of this local variable will be available from any part of the special function start().
32.2(1). Assignment operator is executed. The last available Bid price will be assigned to the variable Price (for example at the first tick it is equal to 1.2744).
33(1). Next comes My_Function() call:
My_Function();
This line will be executed within the start() operation. The result of this code part implementation (custom function call) is passing control to the function body (description) with further returning it to the call place.
34(1). There is only one operator in the custom function description:
Count++;
At the first custom function call Count is equal to zero. As the result of Count++ operator execution the value of Count will be increased by one. Having executed this operator (the only and the last one) the custom function finishes its operation and returns control to the place, from which it has been called.
It should be noted here that custom functions may be called only from special functions (or from other custom functions that are called from special functions). That is why the following statement is correct: at any current moment one of the special functions is operating (or start() is waiting for a new tick to be then started by the client terminal) and custom functions are executed only inside special functions.
In this case control is returned to the special function start() that is being executed, i.e. to the line following the function call operator:
35(1). This line contains Alert() call:
Alert ("New tick ",Count," Price = ",Price);
The function Alert() will show in a window all constant and variable enumerated in brackets:
| New tick 1 Price = 1.2744 |
36(1). Operator
return;
finishes start() operation.
37. Control is passed to the client terminal waiting for a new tick.
At further start() executions variables will get new values and messages by Alert() will be shown, i.e. the program will perform points 32 - 36. At each start() execution (at each tick) call to the custom function My_Function will be performed and this function will be executed. The execution of start() will continue until a user decides to terminate the program operation. In this case the special function deinit() will be executed and the program will stop operating.
The program userfunction.ех4 started for execution will show a window containing messages by Alert(). Note, the result of the program operation will be the same as the result of a simple
Expert Advisor simple.mq4 operation. It is clear that userfunction.mq4
structure is made up in accordance with a usual order
of functional blocks location. If another acceptable order is used, the result will be the same.