10.2 Global variables

If you assign a value to a variable for the first time, then this variable will automatically become global if you did not explicitly tell Hollywood that it shall be local by using the Local statement. Global variables can be accessed from anywhere in your script. They are globally available to all functions. However, if there is a local variable that has the same name as a global variable, then Hollywood will always use this local variable first.

Global variables are slower than local variables and they cannot be easily collected by the garbage collector unless you explicitly set them to Nil when you do not need them any longer. Thus, you should only use globals when really necessary. In functions you should try to work with local variables only.

Here is an example:

 
; bad code!
Function p_Add(a, b)
    tmp = a + b
    Return(tmp)
EndFunction

The variable tmp will be created as a global variable. This does not make much sense here because you only need the variable tmp in this function. So you should better make it local to this function, e.g.

 
; good code!
Function p_Add(a, b)
    Local tmp = a + b
    Return(tmp)
EndFunction

To improve the readability of your program, you can use the Global statement to clearly mark certain variables as globals. This is of course optional, because all variables that are not explicitly declared as local will become global automatically. But using the Global statement makes your program more readable.

See Local variables for details.


Show TOC