Advertisements
$ £ ¥
¥ £ $

Definition and Use of Functions in MQL4

For people new to programming, functions may seem tricky and it could take some time to fully understand and appreciate their power. As you may probably know already, a program is a sequence of actions. And with functions, you can group some actions together to execute a task. This guide explains what these functions are and how to use them in MQL4.

We can define a function as a body of code that is returning a value or executing a task.

There are several reasons why you should use functions. Among those reasons we find making the code more readable and even more important, modularity. With functions you can create code that can be easily re-used, which is very handy when you need to repeat some tasks and operations over and over.

To understand functions, you must have already clear what variables and comments are, you should also been able to test your code with a demo script.

In our definition of function, we said that a function is a body of code that returns a value or executes a task. This may be confusing but some example will help making sense of it.

First of all, a function must be defined so that MetaTrader is aware of it. The definition of a function is the equivalent of the declaration of a variable — you just inform MetaEditor about the existence of something. Definition is also to instruct MetaTrader about what the function does, the action performed. There can only be one definition of a function with the same name in a program.

MQL4 has two types of functions: one that performs a task and returns a value, and one that performs a task but doesn't return anything. There is no rule on when to use one or the other — this totally depends on the purpose of the function.

When you define a function you need to specify what is the type of data that is returned:

data_type function_name(argument1, argument2, ..., argumentx)
{
   task;
   return(value);
}

When you define a function with no return of value, you just use void for its data type:

void function_name(argument1, argument2, ..., argumentx)
{
   task;
}

Once a function is defined, you can call it. To call a function means to trigger it and execute the associated action. You will see some examples further.

Functions can take arguments. Arguments are input values for the the function, so that some actions inside the function can be calculated depending on external parameters. You pass the parameters to a function while calling it.

If you read our guide on creating a demo script, you could notice a function OnStart(). It is one of the default functions in MetaTrader (you will learn about other native functions in other articles). It is defined as void, so it returns nothing, and with no arguments. The function is called when the script is executed on a chart and the code inside it is executed by MetaTrader.

// Definition of the OnStart() function as void (no return of a value) and with no arguments.
void OnStart()
{
   Alert("Hello World!");
}

Now we can see some examples of custom functions that you can create. These examples are of very simple functions, but remember that functions can be very powerful and perform very complex tasks.

// First, we define a function of the name Sum, which returns an int value and accepts two int parameters, a and b.
int Sum(int a, int b)
{
   // Inside the function's body, we can declare other variables, in this case an int s, which is the sum of a and b.
   int s = a + b;
   // Then the function returns the value of s.
   return(s);
}

// The function OnStart() is called by MetaTrader when the script is attached to a chart.
// OnStart() has no parameters and returns no value.
void OnStart()
{
   // total as a variable of type int which is assigned the sum of 1 and 2.
   // Sum is called.
   int total = Sum(1, 2);
   
   // The variable total is printed.
   Print(total);
   
   // Sum can be called also from another function, in this case Print.
   // The sum of 10 and 20 is printed.
   Print(Sum(10, 20));
}
Output of MQL4 Function Demo Script in MT4
Example of MQL4 Function #1 - Result
// First we define a function of name Sum, which doesn't return a value but only performs and action.
// Sum accepts two int parameters, a and b.
void Sum(int a, int b)
{
   // Inside the function, we can declare other variables, in this case an int s, which is the sum of a and b.
   int s = a + b;
   // Sum prints the value of s.
   Print(s);
}

// The function OnStart() is called by MetaTrader when the script is attached to a chart.
// OnStart() has no parameters and returns no value.
void OnStart()
{
   // Sum is called and it will execute its task.
   Sum(10, 10);
}
Output of MQL4 Function Without Return Value in MT4
Example of MQL4 Function #2 - Result
// We define a CreateHello function, which returns a string, with an argument name of type string.
string CreateHello(string name)
{
   // The hello is created attaching the name to "Hello".
   string s = "Hello " + name;
   // Sum prints the value of s.
   return s;
}

// The function OnStart() is called by MetaTrader when the script is attached to a chart.
// OnStart() has no parameters and returns no value.
void OnStart()
{
   // We print the Hello, calling the function Print and then creating the string with the function.
   Print(CreateHello("John"));
}
Calling a Function from a Function to Append a String and Print the Result Out
Example of MQL4 Function #3 - Result

With the above, you have seen some simple examples of definition and use of functions. The suggestion is to try, test, experiment, and become more confident with their use, because you will certainly benefit from using functions in your code.

If you want to get news of the most recent updates to our guides or anything else related to Forex trading, you can subscribe to our monthly newsletter.