As to the necessity to specify functions in a program, they can be divided into 2 groups:
Functions that are not described in a program, and functions that must be described in a program. Standard functions are not described in programs. User-defined functions must be described in any program. Special functions, if any, must be described in a program, too.
Format of Function Description
A function description consists of two basic parts: function header and function body.
The function header contains the type of the return value, the function name, and the list of formal parameters enclosed in parentheses. If a function must not return any value, its type should be named void.
The function body can consist of simple and/or compound operators and calls to other functions, and is enclosed in parentheses.
Return_value_type Function_name (List of formal parameters)//Header { // Opening brace
Program code // A function body may consist ..
composing the function //.. of operators and ..
body //.. calls to other functions } // Closing brace
The parameters in the list are given separated by commas. The amount of parameters passed to function is limited and cannot exceed 64. As formal parameters in the function header, you can specify only variables (but not constants, other function calls or expressions). The
amount, type and order of the passed parameters in the function call must be the same as those of formal parameters specified in the function description (the only exception will be the call to a function that has parameters with the default values):
int My_function (int a, double b) // Example of function description {
int c = a * b + 3; // Function body operator return (c); // Function exit operator }
// Here (from left to right in the header):
int // Return value type
My_function // Function name
int a // First formal parameter a of the int type
double b // Second formal parameter b of the double type
Parameters passed to the function can have default values that are defined by the constant of the corresponding type:
int My_function (int a, bool b=true, int c=1, double d=0.5)//Example of function description
If there are actual parameters given in the call to the function with default values, the values of the actual parameters will be calculated in the function. If there are no actual parameters given in the call to the function with default values, the corresponding default values will be calculated.
Special functions can have parameters, too. However, the client terminal does not pass any parameters from outside when calling these functions, it just uses the default values. Special functions can be called from any location of the module according to general rules, on a par with other functions.
Function Execution Rules
Location for a function description in a program:
The function description must be placed in your program separately, outside any other functions (i.e., it must not be located in another function).
Function execution:
A function called for execution is executed according the code that composes the function body.
Format of Operator 'return'
The value returned by the function is the value of the parameter specified in the parentheses of the operator 'return'. The operator 'return' consists of the key word 'return', the Expression enclosed in parentheses, and ends in the character of ";" (semicolon). The full-format operator 'return':
return (Expression); // Operator return
The expression in parentheses can be a constant, a variable or a function call. The type of the value returned using the operator 'return' must be the same as the type of the function return value specified in the function header. If this is not the case, the value of the expression specified in the operator 'return' should be cast to the type of the return value specified in the header of the function description. If the typecasting is impossible, MetaEditor will give you an error message when compiling your program.
Execution Rule of Operator 'return'
The operator 'return' stops the execution of the nearest external function and passes the control to the calling program according to the rules defined for a function call. The value returned by the function is the value of the expression specified in the operator 'return'. If the type of the parameter value specified in the operator 'return' is other than that of the return value specified in the function header, the value must be cast to the type of the return value
specified in the header.
An example of how to use the operator 'return' that returns a value:
bool My_function (int Alpha) // Description of the
If the return value of a function is of the void type, you should use the operator 'return' without expression:
return; // Operator 'return' without the expressions in parentheses
An example of usage of the operator 'return' without return value:
void My_function (double Price_Sell) // Description of the user-defined function
{ // Start of the function body
if(Price_Sell-Ask >100 * Point) // Operator 'if' Alert("Profit for this order exceeds 100 п");// Standard function call
return; // Exit function
} // End of the function body
It is allowed not include the operator 'return' in a function description. In this case, the function will terminate its operation automatically, as soon as (according to the executing algorithm) the last operator is executed in the function body. An example of the description of a function without the operator 'return':
In this case, the function will complete its operations at the moment when the cycle operator 'for' finishes its execution. The last action at the function execution will be the condition test in the cycle operator. As soon as the Condition in the header of the cycle operator 'for' becomes false, the control will be passed outside the cycle operator. However, for the reason that the cycle operator is the last executable operator in the body of the function named My_function (), the user-defined function will terminate its operations, whereas the control will be passed outside the function, to the location, from which the function was called for execution.
Variables
For creating programs in any algorithmic language knowing different variable types is very important. In this section we will analyze all types of variables used in MQL4.
Predefined Variables and RefreshRates Function.
First of all predefined variables should be learned. Names of predefined variables are reserved and cannot be used for creating custom variables.
It is predefined variables that bear the main information necessary for analyzing current market situation. For updating this information RefreshRates() function is used.
Types of Variables.Variables are very important in writing a program. They are divided into local and global, external and internal. Statistic variables preserve their values between function calls, it is useful for remembering some values of local variables without creating global variables.
GlobalVariables.
Beside global variables on the level of a separate program, the values of which are available from any part of the program, there are global
variables on terminal level. Such global variables are called
GlobalVariables. They make it possible to establish interaction between independent parts of programs written in MQL4. They can be used for sharing values between scripts, indicators and Expert Advisors. At terminal closing values of GlobalVariables are also preserved for being available at the next MetaTrader 4 start. It must be remembered that if there were no calls to a global variable during 4 weeks, it will be deleted.
Arrays.
If you need to save or process large volumes of one-type values, you cannot do without arrays. Before using an array, it should be declared like a variable. Calling of array elements is performed via specifying index(es) of an element. Array indexation starts from zero. Number of array
dimensions is called dimensionality. Arrays larger than four-dimensional ones are not accepted. Array values must be clearly initialized in order to avoid difficult-to-locate errors.