Statements, code blocks, and functions

Thus, in the script generated by the Wizard, the OnStart function appears as follows:

void OnStart()
{
}

It is exactly our first subject matter within the context of programming in MQL5. Here again, we immediately encounter unknown concepts and character sequences. To explain them, we shall make a short digression.

A program must usually implement the following typical stages when running:

  • Defining variables, i.e., named cells in the computer memory to store data;
  • Organizing the source data input;
  • Processing the data — an applied algorithm; and
  • Organizing the output of results.

All these stages are not necessary in terms of maintaining the syntactic correctness of the program. For example, if we create a program that computes the product of "2*2", it obviously does not need any input data, because numbers necessary for multiplying are integrated in the program text. Moreover, since 2 and 2 are constant values in this expression, no named cells (variables) are required in the program. Since we know it anyway what twice two is, we don't really need to communicate the product number. Such a program would lack any real function, of course. However, it would be absolutely correct from a technical point of view.

More interestingly, the program may contain no statements on processing. Our script template specifically represents a sample 'null' program. But what is the above text fragment?

In his day, Niklaus Wirth, one of the big names in programming, gave a simple generalized definition of programming as a symbiosis of algorithms and data structures.

"Algorithm" shall mean a sequence of statements of a particular programming language. A statement is a kind of sentence, i.e., a completed utterance, articulated in a programming language according to its syntax rules. The name "statement" itself suggests that it is perceived by computers as a guide to operations. In other words, statements describe when and how the required applied data structures shall be processed. This is exactly why the interpenetration of algorithms and data structures allows putting the author's ideas into practice.

Unfortunately, in most practical tasks, the number of statements is so large that they must be systematized somehow for the human to recognize and control the program behavior.

Here too, the divide-and-conquer algorithm comes to help, which is used practically everywhere in programming and in different guises. We will learn all of them as we continue in this book, now just noting the essence.

As known, the algorithm reduces to dividing a large complex task into smaller and simpler ones. Here, we can compare this process with constructing a house or assembling a spacecraft. Both "products" consist of multiple different modules that, in turn, consist of components, and the latter ones of even smaller parts, etc.

Extending this similarity to algorithms, we can say that statements are small parts, while the entire program is a house/spacecraft. Therefore, we need structural blocks sized intermediately.

This is why it is customary, when implementing algorithms, to combine logically related statements into larger named fragments, the functions. In the required places of the program, we can address the function by its name (call it) and doing so, ask the computer to execute all statements contained inside the function. The entire program is, in fact, the largest external block and therefore, it can also be presented by the function, from which smaller functions are called or statements are executed immediately if they are not many. Now we're approaching the OnStart function.

Name OnStart is reserved in scripts to denote the ultimate function that is called by the terminal itself as a response to the user's actions when the user launches the script using the context menu command or dragging the mouse over the chart. Thus, the preceding fragment of the code defines the function OnStart that predetermines the behavior of our entire script.

Those who know programming in other languages, such as C, C++, Rust, or Kotlin, can notice the similarity of this function with the function main – the core point of entering into the program.

Any script must contain the function OnStart. Otherwise, the compilation may finish with an error.

Empty function OnStart, as ours, starts being executed by the terminal (as soon as the script is launched in any manner) and immediately finishes its operation. Strictly speaking, there is no applied algorithm in our script yet, but there is already a stub function to add it.

In other types of MQL programs, there are also special functions to be defined by the programmer in their code. We will get into the specific features in the relevant parts of the book.

We will consider the function definition syntax in detail in the second part of this book. For a hands-on review of it, it is sufficient to mention the following basic essentials to understand the description of OnStart.

Since functions are usually intended for obtaining an applicable result, the characteristics of the expected value are described in a special manner in their definition: What data types should be obtained and whether the data is even necessary. Some functions can perform actions that do not require returning the value. For example, a function can be intended for changing the settings of the current chart or to send push notifications when the predefined drawdown level is reached on the account. All this can be programmed by the statements in the function, and it does not create any new data (reasonable to be returned to any other parts of the program).

In our case, the situation is similar: As the main function of the script, OnStart could return its result to the external environment only (directly into the terminal) when completed, but this would not affect the operation of the script itself in any way (because it has already finished off).

That is exactly why, before the OnStart function name, there is the word void that informs the compiler that the result is not important to us (void means emptiness). void is one of many procedure words reserved in MQL5. The compiler knows the meanings of all reserved words, and it is guided by them in reviewing the source code. Particularly, a programmer may use reserved words to define new terms for the compiler, such as the function OnStart itself.

Parentheses after the name are integral to the description of any function: They may enclose the list of function parameters. For instance, if we were writing a function taking a square of a number, we would have to provide it with one parameter for that number. Then we could call this function from any part of the program, having sent one argument over it, i.e., the specific value for the parameter. We will see later how to describe the list of parameters; it is not in this current example. This requirement is posed on the function OnStart for it is called by the terminal itself, and it never sends anything to this function as parameters.

At last, braces are used to mark the beginning and the end of the block containing statements. Immediately following the function name string, such a block will contain a set of operations performed by this function. It is also named the function body. In this case, there is nothing inside the braces. Therefore, the script template does not do anything.

The above sequence of word void, name OnStart, an empty list of parameters, and an empty code block defines the least, empty implementation of the function OnStart for the compiler. Later, adding statements into the function body, we will extend the definition of function OnStart.

Having executed the Compile command, we will make sure that the script can be successfully compiled, and that the ready program appears in the Navigator of the terminal in the folder Scripts/MQL5Book/p1. This results from the fact that, on the disk in the relevant folder, there is now the file of Hello.ex5. It can easily be checked in any file manager.

We can run the script on a chart, but the only confirmation of its execution will be the entries in the terminal log (tab Log in the Tools window; not to be mixed with the toolbar):

Scripts        script Hello (EURUSD,H1) loaded successfully
Scripts        script Hello (EURUSD,H1) removed

That is, the script is loaded, the control is sent to the function OnStart, but immediately returned to the terminal because the function does not do anything, and after that, the terminal unloaded the script from the chart.