Self-learning the MQL5 language from scratch - page 44

 
Vladimir Simakov:

Global objects, are initialized with default constructor. For primitive (in our case, all except string) types, it is 0. But for memory (read variables) allocated on the stack, they are not initialized. That's why global variables may be not initialized, remember that in this case they will equal zero. But absence of initialization (at the same time, get used to avoiding compiler-generated warnings right away, unless you know exactly what you're doing) is a serious issue, because reading an uninitialized variable leads to undefined behavior. For example, this code behaves differently in release and debug builds and nobody can guarantee that when you change the compiler version or optimization settings, its behavior will not change too:

Hello, Vladimir! Thank you for your clarification.

Sincerely, Vladimir.

 
MrBrooklin:

Peter, on the subject of global variables, I haven't yet found in the literature that global variables need to be predefined. Do you have a link to the source to improve your knowledge in this matter? The MQL5 Reference does not contain an explicit reference to initialization:

The booltype is designed to store the logical values true or false, whose numeric representation is 1 or 0 respectively .

Examples:

bool a =true;
bool b =false;
bool c =1;

The internal representation is a 1-byte integer number. It should be noted that in boolean expressions, it is acceptable to use other integer or real types or expressions of these types instead of bool, and the compiler will not generate an error. In this case, zero will be interpreted as false and all other values as true.

Regards, Vladimir.
It's desirable to explicitly initialize global variables, at least to get used to doing so. Again, in mql4 you can not initialize variables and arrays in functions at all, but in mql5 you have to do it, otherwise the variables will have "rubbish".
 

I've added it to the script code:

//--- Зададим глобальные переменные:
//переменная enough_time (достаточно времени), где bool - логическое значение: истина (true) или ложь (false)
bool enough_time=false;
//переменная enough_patience (достаточно терпения), где bool - логическое значение: истина (true) или ложь (false)
bool enough_patience=false;

Now in the comments of the script, somehow clearly spelled out for a 1st grade programming school student why the predefined value for bool data type should be exactly false and not true.

Regards, Vladimir.

 
MrBrooklin:

Added it to the script code:

Respectfully, Vladimir.

I don't have anything else to complain about. :)
 
MrBrooklin:

I continue studying the MQL5 programming language and am posting the code of a script, which is a continuation of one task from the participants of this thread. The script has been tested in all modes. No problems were detected. Applied the minimum number of input parameters to start with. The script code is written in English, the comments to the code are in Russian, to ease the learning process. As I promised earlier, I tried to describe the script in a manner comprehensible to a pupil of the 1-st form of programming school.

Best regards, Vladimir.

Vladimir, you should not write it that way. Do not short-circuit the work of functions on external variables. The only exception for you so far is taking values from input variables.
 
Vasiliy Sokolov:
Vladimir, you should not write it that way. Do not lock functions in external variables. The only exception for you so far is taking values from input variables.

Hello Vasily! To be honest, I didn't quite understand the meaning of what is in bold. Please elaborate on what you meant by it.

Sincerely, Vladimir.

 
MrBrooklin:

Hello Vasily! Honestly, I didn't quite understand the meaning of what was written in bold. Please elaborate on what you meant by it.

Sincerely, Vladimir.

Don't use global variables. I will write later on why they should not be used.
 
Vasiliy Sokolov:
Don't use global variables. Why you shouldn't use them I'll write later.

There's a good anecdote about this:

- Question: what is the best type for a global variable in c++?

- Answer: //

 
Vasiliy Sokolov:
Do not use global variables. I will write later why they should not be used.

Now I understand. Thank you, I'll wait to hear from you why you can't use global variables.

Regards, Vladimir.

 
At this stage, until the transition to OOP programming, the use of global variables is fully justified and there is no reason to turn away from them. They are convenient and easy to use anywhere in the program. Why should they not be used? Imho.