MQL4 - automated forex trading   /  

Documentation

MQL4 Reference  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.

Example:

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.

Example:

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.

Example:

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")
  {
  }