![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
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.
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.
I've added it to the script code:
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.
Added it to the script code:
Respectfully, Vladimir.
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 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.
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. 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: //
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.