Declaring Variables in Function

Back to topics list To post a new topic, please log in or register
avatar
12
Dadash 2015.08.02 13:26
 
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) 

thanks in advance

avatar
Moderator
3423
GumRai 2015.08.02 13:53 #
 
Dadash:

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
avatar
12
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?

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



avatar
Moderator
3423
GumRai 2015.08.02 15:43 #
 

 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
avatar
12
Dadash 2015.08.02 17:35 #
 

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);
}
}

 

 

avatar
13009
WHRoeder 2015.08.02 19:58 #
 
Dadash:
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

avatar
12
Dadash 2015.08.02 22:00 #
 

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.

 

avatar
Moderator
3423
GumRai 2015.08.02 23:38 #
 

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

avatar
12
Dadash 2015.08.03 08:58 #
 

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? 

avatar
Moderator
3423
GumRai 2015.08.03 09:48 #
 
Dadash:

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? 

Yes, the original code will almost always return 1 or 2 with the first call. It cannot return 0 unless line1==line2
avatar
12
Dadash 2015.08.03 12:18 #
 
thanks guys, for your responses and of course patience!
Back to topics list  

To add comments, please log in or register