# Declaring Variables in Function

37

```int Crossed (double line1 , double line2)
{
static int last_direction = 0;
static int current_direction = 0;
if(line1>line2)current_direction = 1; //up
if(line1<line2)current_direction = 2; //down
if(current_direction != last_direction) //changed
{
last_direction = current_direction;
return (last_direction);
}
else
{
return (0);
}
}```

Dear programmers

about above function which is in Coders’ guru MQL4 tutorials, i have a question.

wonder if each time the main code calls this function, reads two declaring static variable highlighted and gives zero value to last_direction and current_direction? if so, the third if tester always is true (last_direction=0 and current_direction=1 or 2)

Moderator
10617

wonder if each time the main code calls this function, reads two declaring static variable highlighted and gives zero value to last_direction and current_direction? if so, the third if tester always is true (last_direction=0 and current_direction=1 or 2)

Sorry, this just reads as gibberish to me
37

i mean does the function assign zero value to last_direction and current_direction variables at beginning in each call?

in other words, it seems that function replaces the values of last_direction and current_direction with 0 in every implementation.

if this is true, then every time the tester (current_direction != last_direction) is true. because we have 0 for last_direction  and 1 or 2 for current_direction.

hope that I am clear. cause I'm not comfortable with English to explain clearer :(

Moderator
10617

Dadash 2015.08.02 14:54 #

i mean does the function assign zero value to last_direction and current_direction variables at beginning in each call?

No, a static variable retains the last value that it was assigned
37

Thanks GumRai, But let me explain what happens when function runs in my sight.

below i commented the sequence. are they right? if so this is what happens in first run of function.  and of course next times we call function. every time function returns 1 or 2 which means it says line1 and line2 makes cross in every tik. and never returns 0.

```int Crossed (double line1 , double line2)
{
static int last_direction = 0;                        //assigns 0 value to last_direction
static int current_direction = 0;                     //assigns 0 value to current_direction
if(line1>line2)current_direction = 1;                 //if Line1 is above Line2, then assigns 1 to current_direction
if(line1<line2)current_direction = 2;                 //if Line2 is above Line1, then assigns 2 to current_direction
if(current_direction != last_direction)               //current_direction = 1 or 2 right? and last_direction = 0. so obviously current_direction != last_direction.
{
last_direction = current_direction;                   //cause test was true, then last_direction = 1 or 2.
return (last_direction);                              //so function returns 1 or 2.
}
else
{
return (0);
}
}```
16076

```static int last_direction = 0;          //assigns 0 value to last_direction    When initially loaded on to chart.
static int current_direction = 0;       //assigns 0 value to current_direction When initially loaded on to chart.
if(line1>line2)current_direction = 1;   //if Line1 is above Line2, then assigns 1 to current_direction
if(line1<line2)current_direction = 2;   //if Line2 is above Line1, then assigns 2 to current_direction
if(current_direction != last_direction) //current_direction = 1 or 2 right?    Unless line1 == line2```
37

thanks a lot WHRoeder

first part of my problem solved.

"We have initialized them (last_direction & current_direction) to 0 because we don’t want them to work in the first call to the function (if they worked in the first call the expert advisor will open an order as soon as we load it in the terminal)" coder says in his tutorial. but it seems when function initially loads on to chart, which means first tik, the result well be 1 or 2 unless line1 == line2 which is not common. (line1: EMA_Fast, line2: EMA_Slow) and when function returns 1 or 2, leads the expert to open an order.

Moderator
10617

It is not clear from your posts whether your problem is that the function always returns 1 or 2 every call or just on the first call.

```int Crossed(double line1,double line2)
{
static int last_direction = 0;              //assigns 0 value to last_direction
static int current_direction = 0;           //assigns 0 value to current_direction
if(line1>line2)current_direction = 1;       //if Line1 is above Line2, then assigns 1 to current_direction
if(line1<line2)current_direction = 2;       //if Line2 is above Line1, then assigns 2 to current_direction
if(current_direction != last_direction)     //current_direction = 1 or 2 right? and last_direction = 0. so obviously current_direction != last_direction.
{
if(last_direction==0)
{
last_direction=current_direction;
return(0);
}
else
{
last_direction=current_direction;
return (last_direction);
}
}
return (0);
}```

will return 0 on the first call

37

Thanks GumRai

the function you modified returns 0 at first call. but do you agree that code i wrote does not return 0 at first call?

Moderator
10617