MQL4 TUTORIAL BASICS – 61 SIMPLE OVERLOADED FUNCTION

video

In this video we are going to create an overloaded function that is able to take two or three parameters, so let’s find out how to do that with mql4.
To get started please click on the little button here or press F4 on your keyboard, now you should see the Metaeditor window and here you want to click on file, new file, expert advisor from template, continue, I will call this file simple function overloading, click on continue, continue and finish.
Now you can delete everything above the ontick function and the two comment lines here.
We start by calculating a difference that is a double value and we get it when we subtract the bid price from the ask price. Afterwards, we want to call our user-defined function that one will be called add values, it will take two parameters, the ask price and the bid price and it will return a result value where the two values from the two parameters are added and later we will create another overloaded function with the same name but the overloaded version will be able to take three parameters.
Inside of the ontick function we need to add a comment statement to output the text, the result is, followed by the calculated value and this function doesn’t exist so far so we need to create it now, the return type will be double, the name of the function is add values and it takes two parameters, the first one is for the ask price, the second one is for the bid price and if you ask yourself why this name is not the same as this one, well we are free to use any name we want inside of the user-defined function.
I would like to do a print statement inside of the journal tab, this one should output that we are inside of the function that can handle two parameter values, it doesn’t do very much it just is going to create a return value by adding the ask value and the bid value and afterwards, we are going to use the return statement to return the value that we have calculated to the main function.
That was very easy!
You can compile the code now and you shouldn’t get any errors but so far we have no overloaded function and that’s why we are going to create the next function with the exact same name but this version can handle three parameters, the third parameter is the difference value.
Once again I would like to use the print statement to output the text, this is the function that takes three parameter values, this function will add all three parameters and it also will return the calculated value to the main function.
Now let’s click on the compile button, in my case I don’t have any errors and no warnings but if you don’t have an understanding what all the code here does or if this was too fast for you maybe you want to watch one of the other videos in this basic video series first or maybe even the premium course on our website might be interesting for you. Actually, it was a premium course member that suggested this topic for video and if you are a premium course member already and if you have some ideas that could become such a video here just send me an email.
Okay, so why did we do all this here?
Well, if you cut out the parameter part here you will see that the help function will show us that we can use one of two different versions for this function that is the overloading. The first function will take the ask value and the bid value and when you click here you will see that the second function is able to handle three parameters, so let’s find out if this works as designed.
To do that please click on a little button here or press F4 on your keyboard, now you should see the Metaeditor window and here you want to click on view, strategy tester or press control and r, please pick the new file, simple function overloading dot ex4, mark the visual mode option here and start your test.
Here we are! The expert advisor is running, it returns a result and when we click on the journal tab we see that we are using the function that takes two parameter values, now let’s go back to Metaeditor and add a comma here and now we see the tooltip text that we can use a third value and that’s where we want to pass the difference that we have calculated here.
Now please recompile the code, go back to Metatrader, stop the current test, restart it, we don’t see any difference here but when you click on the journal tab you will see that the expert advisor is now going to use the function that takes three parameter values so the overloaded function now works and in this little video you have learned how to create an overloaded function for Metatrader 4 and you have coded it yourself with a few lines of mql4 code.