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.
 
Dominik Egert #:

I somewhat disagree that a preprocessor directive can be mentioned in the context of const variables.

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

const can be very useful in function signatures because it puts the value of parameters into read-only mode. const is a keyword specially created for the coder. It has no representation in the executable binary created by the compiler.

Unlike the const variable, the #define operator represents a so-called r-value, which is actually located in a read-only memory location in a further executable binary, and therefore cannot actually be changed in any way.

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

Also, the #define directive does not have the same "location" as a global directive, it can appear anywhere in the source file. At the preprocessor stage, the concept of "{...}" code blocks is not available or evaluated.

The visibility of variables is determined by codeblocks. You can at any level define codeblocks any way you want, except at the global level. At the global level, you need some "entity" that the block belongs to. A function, class, structure, or namespace.

Within such a "named" block, you can cascade as many blocks as you want, and visibility will always be only within blocks of the same level or included/subsidiary blocks.

static variables actually reside in the global memory space allocated for the programme, the visibility of this variable is determined by the code block in which it was declared/defined. This can be global space as well. In fact, a variable declared/defined in global space has the implicit keyword "static". Its explicit mention will not change the behaviour of this variable in any way.

In your article, I am missing the keyword "extern" as well as "input". I think they should be part of the article.

Anyway, I think this is a good article, especially for beginners, as the concept of variables is sometimes hard to understand at the beginning and many mistakes can be avoided if implemented correctly.

Maybe you will continue the article, it would be nice if you talk about the definition, declaration and initialisation of variables as well as memory. Perhaps you will also point out the pitfalls if done incorrectly.

And for more understanding, maybe some advanced details about the stack, variable ordering, MSB vs LSB, memory addressing..... Okay, maybe that's too far.


Thanks for sharing the information. I will try to write about what you mentioned as much as I can.

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

By the way, I like the way constants are implemented in C# (the compiler replaces them with literal values). 

https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/constants

In fact, when the compiler encounters a constant identifier in C# source code, it substitutes the literal value directly into the intermediate language (IL) code that it produces. Because there is no variable address associated with a constant at run time,  const  fields cannot be passed by reference and cannot appear as an l-value in an expression.

Judging by the fact that in MQL I can pass a constant by reference, in MQL constants remain variables after compilation.