some basic questions

 

Below I have some basic questions while learning mql4.

Apologize if it is too remedial but I would really appreciate if anyone could help me to understand these questions or at least direct me to the right place, many thanks.


1. difference between "return", "return(0)" & "return(-1)"

for example, if I have a function (or start() ) with 10 lines like below

what happens when "return" or "return(0)" or "return(-1)" is placed after line 5

If it stops, then, where is the position now (e.g. before line 1 or after line 10 or quits the question after line 5?)

what if this is not a function but int start() {......}

void function (int& A, int B, double C) //or int start()
{
line 1
line 2
line 3
line 4
line 5

return //or return(0) or return(-1)

line 6
line 7
line 8
line 9
line 10

}

2. above example, variable A has a "&" sign and B without a "&" sign, what is the difference with/without the "&" sign and how to use them

It would be great if anyone could explain above with a simple example.

with many thanks in advance.

 
  1. difference between "return", "return(0)" & "return(-1)"

    void function(){ ... return; } The function doesn't return anything. Print(function()) or int var = function() would not compile.

    TYPE function(){ ... return(x); } The function returns a TYPE (int, double, string, datetime, or bool)

    Print( "2+3=",Function(2,3) ); // Prints 2+3=5
    int five = Function(2,3);
    :
    int Function(int a, int b){ return(a+b); }

  2. what happens when "return" or "return(0)" or "return(-1)" is placed after line 5

    If it stops, then, where is the position now (e.g. before line 1 or after line 10 or quits the question after line 5?)

    what if this is not a function but int start() {......}
    Lines 6-10 are not executed. The function returns to the caller. The caller of start is the terminal which then waits for the next tick (the returned value is ignored.)
  3. 2. above example, variable A has a "&" sign and B without a "&" sign, what is the difference with/without the "&" sign and how to use them
    The ampersand is call by reference. The function can change the value of the variable
    int x=1, y=2; function(x, y); Print("x=",x," y=",y); // Prints x=2, y=2
    :
    void function(int& a, int b){ 
    a++; b++; // a=2 and b=3 here.
    return; }

  4. Time to hit the books
 
WHRoeder:
  1. Lines 6-10 are not executed. The function returns to the caller. The caller of start is the terminal which then waits for the next tick (the returned value is ignored.)


Hi WHRoeder, thanks for your time

so do I understand correctly that if return (0) is placed after line 5 of int start(){..}, it ignores line 6-10 and waits for the next tick to start from line 1 again?

what about if it is return(-1) in this case?

 
WHRoeder
int x=1, y=2; function(x, y); Print("x=",x," y=",y); // Prints x=2, y=2
:
void function(int& a, int b){ 
a++; b++; // a=2 and b=3 here.
return; }
  1. The ampersand is call by reference. The function can change the value of the variable

if this is the use of ampersand, then why not declare int x as global variable?

is it because if x is declared as global variable, it has a risk of being modified somewhere in the program whereas ampersand ensures only particular function(s) can modify x?

thanks again for your time.

int x;

int init()
{ x=1; return(0);}

int start()
{
 int y=2;
 function(x, y);
return(0);
}

void function(int a, int b){ 
a++; b++; 
return; }
 
holdenmcgorin:

if this is the use of ampersand, then why not declare int x as global variable?

is it because if x is declared as global variable, it has a risk of being modified somewhere in the program whereas ampersand ensures only particular function(s) can modify x?

thanks again for your time.

If you write a Function to perform an action on an array and you use a Globally declared variable then the Function can only be used to work on a specific array . . . if you pass the array name by reference then the Function can be used on any array that you decide to pas to it.
Reason: