/

# MQL4 Book

Subscribe to signal
A FRS USD
80.54%, 541.63 USD
Try product
Gold Mine
Indicator
wajdyss_Stochastic_indicator_v1
Author: wajdyss
Screenshot
EURJPY, M15
Real

MQL4 Book  Basics of MQL4  Constants and Variables

 Book in One File Download it - 2 Mb

# Constants and Variables

The terms of 'constant' and 'variable' are considered in one section since these terms are very close in themselves.

## The Notion of Constant

Constant is a part of a program; an object that has a value.

The term of constant in a program is similar to the same term used in mathematical equations. It is an invariable value. To describe the nature of a constant used in an algorithmic language in as many details as possible, let us refer to well-known physical and mathematical constants.

The human race has discovered the constants, the values of which do not depend on us in any way. Those are, for example, in physics: free fall acceleration that is always equal to 9.8 m/s/s; in mathematics: Pi = 3.14. Constants of the kind cannot be considered similar to constants in an algorithmic language.

The term of constant is also used in mathematical equations. For example, in the equation of Y = 3 * X + 7, numbers 3 and 7 are constants. The values of such constants are fully dependent on the will of the person that has made the equation. This is the closest analogy of constants used in MQL4 programs.

A constant (as a value) is placed by a programmer in the code at the stage of its creation. The constant is characterized only by its value, so the terms of 'constant' and 'the value of a constant' are full synonyms.

Exemplary Constants:
`37, 3.14, true, "Kazan"`

Fig. 4. A Constant in the memory of a PC.

## The Properties of Constants

The property of a constant is its power to retain during the time of the program operation the value set by the programmer and set this value to the program when the program requests this (Fig. 5). For each constant in the program, the computer allocates a part of its memory of the necessary size. The value of a constant cannot be changed during execution of the program neither by programmer nor by computer (Fig. 6).

 The value of the constant remains always the same.

Fig. 5. The state of the memory cell of a constant when setting the value to the program.

 The value of a constant cannot be changed during the program operation.

Fig. 6. It is impossible to change the value of a constant during the program operation.

## The Notion of Variable

Variable is a program part that has a value and a name.

The term of variable in MQL4 is similar to that accepted in mathematics. The difference between them consists only in that the value of a variable in mathematics is always implied, whereas the value of variable in an executing program is stored in a special memory cell in the computer.

The term of 'variable identifier' is the full synonym of 'variable name'. The variable is put into the code text by its author at the stage of coding as a variable name. The name (identifier) of a variable can consist of letters, digits, underscore. However, it must start with a letter. MQL4 is case-sensitive, i.e., S and s are not the same.

Exemplary variable names: Alpha, alFa, beta, NuMbEr, Num, A_37, A37, qwerty_123
The following identifiers of variables represent the names of different variables: a_22 and А_22; Massa and MASSA.
Exemplary values of variables: 37, 3.14, true, "Kazan".

## The Properties of Variable

The property of a variable is its capability to get a certain value from the program, retain it during the period of operation of the program and set this value to the program when requested by the program. For each variable in the program, the computer allocates a part of its memory of the necessary size. Let us refer to Fig. 7 and study the construction of a variable.

Fig. 7. A Variable in the memory of a computer.

There is a value of a variable in the memory cell of the computer. This value can be requested for processing and changed by the program. The name of a variable is never changed. When writing a code, the programmer can set any name for the variable. However, as soon as the ready program is launched, neither programmer nor the computer, nor the program has any technical feasibility to change the name of the variable.

If a program while being executed meets the name of a variable, the program refers to this variable in order to get its value for processing. If a program has referred to a variable, the latter one sets its value to the program. At that, the value of the variable remains the same, whereas the program gets the copy of the value contained in the memory cell allocated for this variable (Fig. 8).

 When the value of a variable is set to a program, this value remains unchanged. The name of a variable will never be changed.

Fig. 8. The state of the memory cell of a variable when setting the value to the program.

A variable is not related to the executing program for a certain period of time. During this period, the program may refer to other variables or make necessary calculations. Between "sessions" of communication with the program, the variable retains its value, i.e., it keeps it unchanged.

According to the algorithm of the program, it can become necessary to change the value of a variable. In this case, the program sets to the variable its new value, whereas the variable gets this value from the program. All necessary modifications are made in the memory cell. This results in that the preceding value of the variable is deleted, whereas a new value of the variable set by the program takes its place (Fig. 9).

 The value of a variable can be changed by the program. The name of the variable is always unchanged.

Fig. 9. The state of the memory cell of a variable when getting the value from the program.

## Exemplary Constants and Variables in a Program

In a program, constants and variables can be found in operators. In the code below, A and B are variables, 7 and 3 are constants:

`A = 7;             // Line 1B = A + 3;         // Line 2`

Let us study how a program works with constants and variables. Executing these lines, the program will make the following steps:

Line 1:

1. Constant 7 sets its value to the program.
2. Variable A gets value 7 from the program.

Line 2:

1. The program has found an expression to the right from the equality sign and is trying to calculate it.
2. Constant 3 sets its value to the program.
3. The program refers to variable A by the name.
4. Variable A sets its value (7) to the program.
5. The program makes calculations (7 + 3).
6. Variable В gets value 10 from the program.

The value of a variable can be changed during the program operation. For example, there can be a line in the program that contains the following:

`В = 33;            // Line 3`

In this case, the following will be done at execution of the program:

1. Constant 33 sets its value to the program.
2. Variable B gets (new) value 33 from the program.

It is easy to notice that variable B gets value 10 at a certain stage of the program execution, and then it gets the value of 33. The name of variable B remains unchanged during all these events, whereas the value of the variable will change.

Fig. 10 shows constants and variables in the program code:

Fig. 10. A constant and a variable in a program.