When should I declare a variable as a static variable (rather than just a normal / local variable) ?

 
Hello,

If we define a variable just after start() function and within the start() block {}, can we call this variable anywhere within the start function block? If we want the expert advisor/computer to remember this variable value must we define this variable as a static variable?

For example if we have:

Start()
{

int Var1;

if time = 12:34:58 then Var1=Ask Price;

if Var1 > 1.2850 then PLace Buy order;

return(0)

}


In the above example, if the current time is 12:34:58 and at this time we call Var1 within the start block, would we get the Ask price when the time was 12:34:58 (let say the ask price at 12:34:58 was 1.2855)?

If we call Var1 the second time and the time is now 10 seconds later ie 12:35:08, would the value for Var1 be zero or still 1.2855?

If we definine Var1 as a local variable by writing "int Var1" after start function then if we call Var1 the second time after the start block has finished executing would the Var1 variable be put back to zero?

If we define Var1 as a static variable then the second time we call Var1, when the time is 12:35:08, would we get the same value as the first time when we called it when time was 12:34:58 ie price of 1.2855?

I ask this question as in MT3 we did not have local and static variables, and all variables seemed to be permanent no matter how many times the expert looped.

Given all of this, in MT4 is it ok to define " all " variables as static variables so they are never lost (just to be safe)? So instead of writing int Var1; Int Var2; in our expert advisors, why not write static int Var1; and static int Var2; etc. for all variables instead? What is the harm in declaring all variable as static variables instead of just local (normal) variables?

Thank you in advance for your help.

Regards

RJF
 
If variable is local and non-static then its value is undefined until it is assigned explicitly.

If variable is external or local static (external variables are static always) then it keeps last assigned value.
 
thank you Slawa
Reason: