problem about static varaible

[Deleted]  

int tempFun()
{
//----
static int temp=10;
temp=temp+10;
//----
return(temp);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+

int start()
{
int temp=tempFun();
Print("the temp =",temp);

return(temp);
}

===========

It seems that the satic variable 'temp' is not initialized. the printed temp is something like 348800,348810 ......

Any help is very appreciated.

[Deleted]  

'Static variables are stored in the permanent memory, their values do not get lost when the function is exited'

Ubzen
5300
Ubzen  

I'm new to coding, however i turned your code into an EA. And the results of the print (in back-test) because the market is closed and i have no ticks coming in is as follows:

 

 

 If that's not the result you're expecting, maybe you can send a print screen of the print result you're getting. Someone with more experience might need more info to determine whats going on. 

William Roeder
25092
William Roeder  

static's are not reinitialized at the deinit/init sequence. So the variable started at 10, and every test case times every tick incremented it.

Don't depend on non-read-only static's being initialized properly, set them in init.

[Deleted]  

When I choose the model of the tester as "Open price only", I can get the expected result, i.e. 20,30,..... But when I change the model to "Every tick", then the results become 348800,348810,..... like i mentioned before. I don't know what wrong is goning on.

William Roeder
25092
William Roeder  
Every call to start() incremented it. open price only, once per bar. Every tick, once per tick.