Discussing the article: "Advanced Variables and Data Types in MQL5"

 

Check out the new article: Advanced Variables and Data Types in MQL5.

Variables and data types are very important topics not only in MQL5 programming but also in any programming language. MQL5 variables and data types can be categorized as simple and advanced ones. In this article, we will identify and learn about advanced ones because we already mentioned simple ones in a previous article.

In this article, we will mention and dive deeper to learn more about variables and data types in MQL5 and how they can be useful when creating or building MQL5 trading software. We will learn more about some advanced concepts of variables and data types and we will cover them through the following topics:

  • Constants: they are identifiers whose unchanged values.
  • Arrays: they are any type of variable with multiple values. 
  • Enumerations: they are integer lists of constants with integer values.
  • Structures: they are a set of related variables with different types.
  • Typecasting: it is the process of converting a type of value to another one.
  • Local variables: they are locally declared variables inside functions.
  • Global variables: they are globally declared variables outside functions.
  • Static variables: they are declared local variables that retain its values in the memory.
  • Predefined variables: they predefined variables by the originator of the programming language.


Author: Mohamed Abdelmaaboud

 
MetaQuotes:

Check out the new article: Advanced Variables and Data Types in MQL5.

Author: Mohamed Abdelmaaboud


I disagree to some extend that a preprocessor directive can be mentioned within the context of const variables.

The keyword const has a different effect than a #define "constant".

const can be very useful in function signatures, as it sets the parameters value passes in to read only mode. const is a keyword especially designed for the coder. There is no representation in the executable binary produced by the compiler.

In contrast to a const variable, a #define statement is a so called r-value, which is actually in a read only memory region later on in the executable binary, and therefore actually cannot be changed what so ever.

Also, #define is a preprocessor stage directive, and actually just replaces all occurrences in the source file. This is done before the compiler even sees the code to be compiled.

In addition, the #define directive has no such "location" as global, it may appear anywhere in the source file. At the stage of the preprocessor, the concept of code-blocks "{...}" is not available, or evaluated.

Visibility of variables is defined by code blocks. You can at any level define code blocks as you like, except at global level. At global level you need some "entity" to which this block belongs. Function, class, struct or namespace.

Inside such a "named" block, you can cascade as many blocks as you like, while the visibility is always only inside the same level or included/child blocks.

static variables actually reside in the global space memory allocated for a program, the visibility of this variable is governed by the code block in which it was declared/defined. This can be also the global space. Actually, a variable declared/defined on global space has an implicit "static" keyword. Mentioning it explicit will not change any of the behaviour of that variable.

I am missing the keyword "extern" in your article, as well as "input". I think these should be part.

Anyways, I think it is a good article, especially for beginners, as the concept of variables is sometimes hard to grasp at the beginning, and lots of errors can be avoided, if properly implemented.

Maybe you continue with a follow-up, would be nice if you could include about defining, declaring and initializing variables, and memory. Maybe also point out the pitfalls if not done correctly.

And for some more insight, maybe some extended details about stack, order of variables, MSB vs LSB, memory addressing.... Ok, maybe this is to far of a fetch.
Reason: