View Single Post
  #9 (permalink)  
Old 01-31-2006, 03:28 PM
codersguru's Avatar
codersguru codersguru is offline
Senior Member
 
Join Date: Oct 2005
Posts: 994
codersguru has a spectacular aura aboutcodersguru has a spectacular aura aboutcodersguru has a spectacular aura about
Arrow Static variables

Quote:
Originally Posted by suffic369
Dear Sir

you have decleared the two static variables inside the block of the function. then every time start function recieves a quote it will call the crossed function,and every time when start calls crossed, crossed will first initilize the two varibles to zero,so how could you compare current_direction with last_direction?

int Crossed (double line1 , double line2)
{
static int last_direction = 0;
static int current_dirction = 0;

if(line1>line2)current_dirction = 1; //up
if(line1<line2)current_dirction = 2; //down



if(current_dirction != last_direction) //changed
{
last_direction = current_dirction;
return (last_direction);
}
else
{
return (0);
}
}
suffic369,

Thanks for the question!


There are two kinds of variables in MQL4, normal variables and static variables;
The static variables are very like the normal variables with only one extra feature:

The initialization of the static variables occurs only one time.
Which means if you write a line of code like that:


static int last_direction = 0;


You mean:

If this is the first call of the program, initialize the variable to 0, else (if it's the second, third, fourth etc call) don't initialize the variable again.


But if you used a normal variable instead of static variable, every time you call your program the initialization of the variable will be occurred.
__________________
Hope it helps !
Coders' Guru
Senior MQL programmer:
www.xpworx.com/custom.htm
Reply With Quote