In my previous blog post, we’ve covered the different data types and how each can be used in a forex expert advisor. Before you’re able to use these though, you must declare and initialize your variables properly first.
It is important to follow the proper syntax in declaration and initialization, as this is your way of introducing your variables to your forex robot. Just think of it as introducing your date to your parents… Your folks are bound to ask a lot of questions and probably doubt the existence of your significant other if you don’t!
Variable declaration refers to the very first mention of the variable in the program during which its data type must be specified while variable initialization specifies the value assigned to it. These actions can be performed separately or in a single line.
Here are some examples of variable declaration:
Here are examples of variable declaration and initialization in one line:
int Age = 35;
bool UpwardCross = true;
double Rainbow = 1.99999;
You can also declare several variables of the same data type in a single line, like so:
int Age, Height, Weight;
It is important to emphasize that an MQL4/MQL5 program will show errors if variables are not properly declared with their corresponding data type. In mentioning the variables in subsequent lines, there is no need to specify its data type again and again. Bear in mind that the value of the variable can change (hence the name variable!) but that its type and name will stay the same throughout the program.
As you’ve probably noticed, a semicolon must always be placed at the end of each line, whether it involves variable declaration or initialization. You can think of it as the general punctuation mark for forex robots. Yep, since emotions don’t exist in the robot world, we don’t make use of any other punctuations commonly used by humans such as the exclamation point or question mark – we just have the semicolon.
We’ll heat things up a notch next week by using these variables in creating operations and expressions. Stay tuned!