Variables on global scope and init

 
I've solved my problem but I'm interested to understand why it behaved the way it did...

I declared a variable on the global scope and also inadvertantly declared it again in init() prior to calculating a value for it in init(). Now, I think I understand why that compiled OK, becasue the second decleration is inside a function (albiet a special one). But I don't understand why the variable wasn't calculated properly by init and the variable is set to 0. Once I removed the second decleration, it worked fine and the correct value was held. My understanding is that global scope declerations are loaded first, init is then loaded once and subsequent calcs will only happen if they are inside start() so my var inside init should not be changed anywhere in the code flow. I recall reading somewhere static variables are better than declerations on the global scope so I will read some more on that, but I'm concerned that my understanding of how an EA loads doesn't fit with reality.

Any advice?
 

Viffer the "version" of the variable that was declared and used inside the function (in this case the init function) should have been calculated properly by init but once you "leave" the init function that variable's value "stayed behind" as would the value of any variable in any called function which was not passed the variable as a reference.

Does that make sense to your situation?

 
Ah, yes, that does make sense... i think... I've declared a variable on global and it's known as VAR i, when I declare the name again inside a function it's known as VAR ii, but I never / can't call VAR ii. I would have to return (VAR ii) to be able to use it. But by not declaring VAR again VAR ii doesn't exist so the function is working with VAR i and I can therefore use the result.

Cool, another lesson ticked. Thanks
Reason: