A local variable is one that is declared inside a function. Local variables are allocated in memory when the function is first run. Once the function has exited, the variable is cleared from memory.
In this example, we'll create a simple function. We'll discuss functions in more detail in Chapter 5. We will declare a local variable inside our function. When the code in this function is run, the variable is declared and used. When the function exits, the variable is cleared from memory:
void myFunction() {
int varInt = 5;
Print(varInt); // Output: 5 }
The name of this function is myFunction(). This function would be called and executed from somewhere else in our program. The variable varInt is local to this function. This is referred to as the variable scope. Local variables cannot be referenced from outside of the function, or anywhere else in the program. They are created when the function is run, and disposed of when the function exits.
Let's take a closer look at variable scope. A local variable's scope is limited to the block that is is declared in. A block is defined as a function or a compound operator within a function. A block is surrounded by opening and closing brackets ({}). Any variables declared inside a block are local only to that block. Let's take a look at a modified example of our function:
void myFunction() {
bool varBool = true; if(varBool == true) { int varInt = 5; Print(varInt); // Output: 5 } }
We've added a new local variable, a boolean variable named varBool. We've also added an if operator block. The if operator, its accompanying brackets and the code inside them are a compound operator. Any variables declared inside the brackets are local to the if operator block.
The if expression can be read as: "If varBool contains the value of true, then execute the code inside the brackets." In this case, the expression is true, so the code inside the brackets is run. The varInt variable is declared, and the value is printed to the log.
The varInt variable is local to the if operator block. That means that varInt cannot be referenced outside the block. Once the block is exited, varInt is out of scope. If we attempt to reference it from outside the if operator block, we'll get a compilation error:
if(varBool == true) {
int varInt = 5;
Print(varInt); // Output: 5 }
varInt = 7; // Both of these expressions Print(varInt); // will produce an error
The expression varInt = 7 will produce an error on compilation. The variable varInt that we declared inside the if operator block is out of scope. If we want to correct this code, we can simply declare the varInt variable that is outside the if operator block:
if(varBool == true) {
int varInt = 5;
Print(varInt); // Output: 5 }
int varInt = 7; // This is a different variable! Print(varInt); // Output: 7
Now we have a variable named varInt in this scope. Note that this is a different variable than the varInt variable declared inside the if operator block, even though they both have the same name!
Here's another example: The variable varInt is declared at the top of the function, and another variable of the same name and type is declared inside the if operator block nested inside the function.
void myFunction() {
bool varBool = true; int varInt = 7; if(varBool == true) { int varInt = 5; Print(varInt); // Output: 5 } Print(varInt); // Output: 7 }
An integer variable named varInt is declared at the top of the function, and assigned a value of 7. A second integer variable named varInt is declared inside the if operator block and initialized with a value of 5. When we print the value of varInt inside the if operator block, it prints a value of 5.
When the if operator block is exited, and we print the value of varInt again, this time it prints a value of 7 – the same value that was declared at the top of the function! In other words, the varInt variable that was declared inside the if operator block overrides the one declared in the scope of the function. By the way, if you try to compile this, you'll get a warning message stating "declaration of 'varInt' hides local declaration..." The example above is not considered good practice, so renaming the second varInt variable would fix the warning.
Just to clarify things, let's see what would happen if we declared the varInt variable once outside of the if operator block, and then referenced it inside the if operator block:
void myFunction() {
bool varBool = true; int varInt = 5; if(varBool == true) { Print(varInt); // Output: 5 } }
This will work as expected, because the varInt variable is declared outside of the if operator block, in the function scope. A variable declared in a higher scope is still in scope inside any nested blocks, as long as those nested blocks don't have a variable of the same name and type.
This may seem arbitrary and confusing, but most modern programming languages treat variable scope this way. By requiring that variables be local only inside the block in which they are declared, programming errors are prevented when variables share the same name. Also note that this is different than the way local variables are handled in MQL4. In MQL4, a local variable declared inside a function is valid anywhere inside that
function, even if it was declared inside a compound operator or block.
Global Variables
A global variable is one that is declared outside of any function. Global variables are defined at the top of your program, generally after the input variables. The scope of a global variable is the entire program.
As demonstrated in the previous section, a local variable declared inside a block will override any variable of the same name and type in a higher scope. If you do this to a global variable, you'll get a compilation warning:
"Declaration of variable hides global declaration." There is no practical reason to override a global variable this way, so watch for this.
The value of a global variable can be changed anywhere in the program, and those changes will be available to all functions in the program. Here's an example:
// Global variable int GlobalVarInt = 5; void functionA() { GlobalVarInt = 7; } void functionB() { Print(GlobalVarInt); // Output: 7 }
The global variable GlobalVarInt is declared at the top of the program, and assigned a value of 5. We'll assume that functionA() is executed first. This changes the value of GlobalVarInt to 7. When functionB() is run, it will print the changed value of GlobalVarInt, which is now 7.
If you have a variable that needs to be accessed by several functions throughout your program, declare it as a global variable at the top of your program. If you have a local variable whose value needs to be retained between function calls, use a static variable instead.