The difference between extern and input - page 6

 

In general, we should start with the fact that global variables are evil, and when such a variable is used in many files and can be changed from anywhere, then it's evil squared! Therefore, such variables should always be declared as constant (unless we are talking about auxiliary variables for debugging purposes, which do not affect the logic of the algorithm).

So, if we have a constant global variable, then we have an obvious conclusion: isn't it easier to add parentheses at the end and make it a function, and thus get flexibility in setting any implementation for this function. And taking into account that extern variables in MQL are underdeveloped, as I wrote above, we have no other alternatives.
 
The extern specifier is needed if the project consists of several mq5 files (not to be confused with mqh). In this case, it makes sense to declare global variables in a separate file. If the project consists of mqh and is built using #include, extern is not needed, because it is one file divided into several files.
 
Vladimir Simakov:
The extern specifier is needed if the project consists of several mq5 files (not to be confused with mqh). In this case, it makes sense to declare global variables in a separate file. If the project consists of mqh and is built using #include, extern is not needed since it is one file divided into several files.

example is needed, I have tried using extern in libraries - it doesn't workhttps://www.mql5.com/ru/forum/316795/page2#comment_12259472

 
Igor Makanu:

example is needed, I have tried using extern in libraries - it doesn't workhttps://www.mql5.com/ru/forum/316795/page2#comment_12259472

It's not for libraries, it's for projects. The project can be built either on #include or make several mq5 files. In the second case, extern is needed to enter global variables into scope of different files.
 
Vladimir Simakov:
Here, in the second case, you need extern, which would enter the global variables in the scope of different files.

I don't mind, but I need an example, here you can guess where to use extern - I tried it in the library, it didn't work, show me how you use extern

 
Vladimir Simakov:
It is not for libraries, but for projects. The project can be built either on #include or make several mq5 files. In the second case, extern is needed to enter global variables into scope of different files.
How exactly do you enter variables into scope? mq5-files in a project are independent, they are not connected in any way. The compilation of each file is independent, isn't it?
 
The whole point is that extern turned out to be simply unnecessary. A variable declared globally in any project file is available in all of its files. But why? Question for the creators.
 
Vladimir Simakov:
The whole point is that extern turned out to be simply unnecessary. A variable declared globally in any project file is available in all of its files. But why? Question for the creators.

You can declare an extern variable in all *.mqh files, in which it is used.

Then, such *.mqh files do not depend on *.mq5 (or other *.mqh) file, in which the global variable is declared.
This allows you to use the files in other projects, increasing code reuse.

 
Ilyas:

You can declare extern variable in all *.mqh files, in which it is used.

Then, such *.mqh files do not depend on the *.mq5 (or other *.mqh) file, in which the global variable is declared.
This allows you to use the files in other projects, increasing code reuse.

Thanks, now I understand the logic. I originally thought it was similar to extern in C++.
 
Vladimir Simakov:
Thanks, now I understand the logic. I originally thought it was similar to extern in C++.

Isn't it similar?