Mnemonix said:
Quote: "All variables declared at the top of your main source code are global as well as arrays and all other variables declared like this
GLOBAL var = value"
In DBPro, variables declared at the top of your program are NOT global just because you place them at the top of your program. You MUST declare them with the Global command to share the values held within them correctly inside and outside of Functions!
(
Important: Make sure you declare the variables you want as Global before assigning values to them, else the values will be be lost when they are initialised with the Global Command - [This is why it's best to do it at the top of your program, first.])
Example:
VARIABLE_1=1 : Rem This Variable is at the top of my program, but has not been declared Global
Global VARIABLE_2
VARIABLE_2=2
Rem Showing the different results inside and outside functions with both Variables:
Print "VARIABLE_1 = ";VARIABLE_1
_ShowVariable(0)
Print
Print "VARIABLE_2 = ";VARIABLE_2
_ShowVariable(1)
Print
Print "VARIABLE_3 should read as 3, but I used the Global command incorrectly, putting it after assigning VARIABLE_3 a value, instead of before."
Print "So, VARIABLE_3 has actually been re-initialised and reads: ";VARIABLE_3
Wait Key
Function _ShowVariable(MADE_GLOBAL)
If MADE_GLOBAL=1
Print "VARIABLE_2 on the other hand WAS declared global and reads correctly as ";VARIABLE_2;" within a function!"
Else
Print "VARIABLE_1 was NOT declared as global, so if I print its value from within a function it reads incorrectly as ";VARIABLE_1;" instead!"
EndIf
EndFunction