MQL4 - automated forex trading   /  

MQL4 Book

Login | Register | Language: ru cn en
Buy Expert Advisors in MetaTrader Market and Let Them Trade For You

MQL4 Book  Operators  Function Description and Operator 'return'

Upgrade to
Book in One File

Function Description and Operator 'return'


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
{
a = a + b*c + d2; // Function body operator
int k = a * 3; // Function body operator
return (k); // 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
bool b // Second formal parameter b of the double type
true // Constant, the default value for b
int c // Third formal parameter c of the int type
1 // Constant, the default value for c
double d // Fourth formal parameter d of the double type
0.5 // Constant, the default value for d

a,b,c,d,k // Local variables

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 user-defined function
{ // Start of the function body
if(Alpha>0) // Operator 'if'
{ // Start of the operator-'if' body
Alert("The value is positive"); // Standard function call
return (true); // First exit from the function
} // End of the operator-'if' body
return (false); // Second exit from the function
} // End of the function body

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':

void My_function (int Alpha)                 // Description of the user-defined function
{ // Start of the function body
for (int i=1; i<=Alpha; i++) // Cycle operator
{ // Start of the cycle body
int a = 2*i + 3; // Assignment operator
Alert ("a=", a); // Standard function call operator
} // End of the cycle body
} // End of the function body

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.