MQL4 Reference Language Basics Variables Extern Variables

Extern variables

The extern storage class defines the external variable. The extern modifier is indicated before the data type.

//--- extern parameters
extern int            MA_Period=13;
extern int            MA_Shift=0;
extern ENUM_MA_METHOD MA_Method=MODE_SMMA;

Similar to input-variables, extern ones also determine the input parameters of an mql4 program. They are available from the Properties window. Unlike input variables, values of extern variables can be modified in the program during its operation. External variables are always reinitialized immediately before the OnInit() is called.

Example:

//--- strict compilation mode
#property strict
//--- show input parameters
#property show_inputs
//--- declare extern and input variables
extern int ExtVar=1;   // ExtVar extern variable
input  int InpVar=2;   // InpVar input variable
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- display the values of ExtVar and InpVar variables
   PrintFormat("Extern=%d, Input=%d",ExtVar,InpVar);
//--- increase the value of ExtVar variable by one
   ExtVar++;
//--- attempt to change the input variable will result in the compilation error
//--- InpVar++;
//--- display the values of ExtVar and InpVar variables
   PrintFormat("Extern=%d, Input=%d",ExtVar,InpVar);
  }

Strict compilation mode with the output of the input parameters window is set in this script. Therefore, the values set in the string comments instead of ExtVar and InpVar variable names are displayed in Variable field.

Extern-variables-strict-mode

Note: Arrays and variables of complex types can't act as extern variables.

Note: The length of a string comment for extern variables cannot exceed 63 characters.

See also

Input Variables, Data Types, Encapsulation and Extensibility of Types, Initialization of Variables, Visibility Scope and Lifetime of Variables, Creating and Deleting Objects