MQL4 Reference

MQL4 Basics:Functions

Function is a named part of a program that can be called from other parts of the program so many times as it is necessary. It consists of type definition for the value to be returned, name, formal parameters, and a composite operator (block) of actions to be performed. Amount of passed parameters is limited and cannot exceed 64.


double                       // type of value to be returned
linfunc (double x, double a, double b) // function name and parameters list
                             // composite operator
   return (a + b);           // returned value

The "return" operator can return the value of the expression included into this operator. If necessary, the expression value can be transformed into the type of function result. A function that does not return values must be of "void" type.


void errmesg(string s)
   Print("error: "+s);

Parameters to be passed to the function can have default values that are defined by constants of the appropriate type.


int somefunc(double a, double d=0.0001, int n=5, bool b=true, string s="passed string")
   Print("Required parameter a=",a);
   Print("The following parameters are transmitted: d=",d," n=",n," b=",b," s=",s);
   return (0);

If the default value was assigned to a parameter, all follow-up parameters must have the default value, too.

Example of a wrong declaration:

int somefunc(double a, double d=0.0001, int n, bool b, string s="passed string")

Function call

If a name that has not been described before appears in an expression and is followed by the left parenthesis, it will be contextually considered as the name of a function.

function_name (x1, x2,..., xn)

Arguments (formal parameters) are passed by value, i.e., each expression xl, . . . , xn is calculated, and the value is passed to the function. The order of expressions calculation and that of values loading are guaranteed. During the execution, the system checks the number and type of arguments given to the function. Such way of addressing to the function is called a value call. Function call is an expression thy value of which is the value returned by the function. The function type described above must correspond with the type of the returned value. The function can be declared or described in any part of the program on the global scope, i.e., outside other functions. The function cannot be declared or described inside of another function.


int start()
   double some_array[4]={0.3, 1.4, 2.5, 3.6};
   double a=linfunc(some_array, 10.5, 8);
double linfunc(double x[], double a, double b)
   return (a*x[0] + b);

At calling of a function with default parameters, the list of parameters to be passed can be limited, but not before the first default parameter.


void somefunc(double init,double sec=0.0001,int level=10);  // function prototype
somefunc();                   // wrong call, the first required parameter must be presented.
somefunc(3.14);               // proper call
somefunc(3.14, 0.0002);       // proper call
somefunc(3.14, 0.0002, 10);   // proper call

When calling a function, one may not skip parameters, even those having default values:

somefunc(3.14, , 10);         // wrong call. the second parameter was skipped.

Special functions

There are three functions with pre-defined names in MQL4:

  • init() is a function to be called during the module initialization. If it is not available, no function will be called at initialization.
  • start() is the basic function. For experts, it is called after the next tick has income. For custom indicators, it is called at recalculation after the indicator has been attached to the chart, at opening of the client terminal (if the indicator is attached to the chart), and after the next tick has income, as well. For scripts, it is executed immediately after the script has been attached to the chart and initialized. If there is no start() function in the module, the module (expert, script, or custom indicator) cannot be launched.
  • deinit() is a function to be called during deinitialization of the module. If it is not available, no function will be called at deinitialization.

Pre-defined functions can have some parameters. However, no parameters will be taken from outside when these functions are called by the client terminal, but the default values will be used. Functions of start(), init(), and deinit() can be called from any point of the module according to the common rules, equally to other functions.

It is not recommended to call start() function from init() function or to perform trade operations, as chart data, market prices, etc. can be incomplete by the moment of the module initialization. The init() and deinit() functions must finish their working as soon as possible and, in no case, shall they loop when trying to start its full-fledged working before the start() function is called.

This article url:

Please login to post comment, if you haven't account you can register free