English

## Declaring Variables in Function

 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
 Moderator3423 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
 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 :(
 Moderator3423 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
 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
 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
 12 Dadash 2015.08.02 22:00 #   thanks a lot WHRoederfirst 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.
 Moderator3423 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
 12 Dadash 2015.08.03 08:58 #   Thanks GumRaithe function you modified returns 0 at first call. but do you agree that code i wrote does not return 0 at first call?
 Moderator3423 GumRai 2015.08.03 09:48 #   Dadash: Thanks GumRaithe 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
 12 Dadash 2015.08.03 12:18 #   thanks guys, for your responses and of course patience!