Popular results

Advertisements
$ £ ¥
¥ £ $

How to Use Constants in MQL4

In some programs, you may want to use variables with values that cannot be changed while the program is running. It is in this case that constants come in. Here we are going to see how to use constants instead of variables in MQL4.

A constant is a variable with a value that cannot be changed while the program is running.

There are a few reasons that could drive you to use constants in your programs. It really depends on the program and the task performed. As a generic rule, if you are sure that a variable's value shouldn't change during the program's runtime, declare it as a constant.

A constant is basically a variable, so what you have learned about declaration and initialization of one is still valid. However, once a value has been assigned to a constant, it cannot be changed.

For a variable to be declared as constant, you need to add const in front of the declaration. We can see an example of code below:

//+------------------------------------------------------------------+
//|                                                       Demo-1.mq4 |
//|                                                    EarnForex.com |
//|                                       https://www.earnforex.com/ |
//+------------------------------------------------------------------+
#property copyright "EarnForex.com"
#property link      "https://www.earnforex.com/"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---

   const int i = 10; // i is declared as an integer constant and initialized with 10.

   Print(i); // The value of i will be printed as output in the Terminal - Experts tab.
   
  }
//+------------------------------------------------------------------+

The code above is perfectly valid, but if we try to change the value of a constant in the code and then we try to compile... BANG! An error!

//+------------------------------------------------------------------+
//|                                                       Demo-1.mq4 |
//|                                                    EarnForex.com |
//|                                       https://www.earnforex.com/ |
//+------------------------------------------------------------------+
#property copyright "EarnForex.com"
#property link      "https://www.earnforex.com/"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---

   const int i = 10; // i is declared as an integer constant and initialized with 10.

   i = 20; // We are trying to change the value of i to 20, this will cause an error.
      
   Print(i); // The value of i will be printed as output in the Terminal - Experts tab.
   
  }
//+------------------------------------------------------------------+
Compilation Error in MetaEditor when Trying to Modify an MQL4 Constant

Remember that you can use constants in your MQL4 code, and in some cases it is highly recommended. It all depends on the purpose of your code.