You should use local variables whenever and wherever possible. They improve the memory management of your program because the garbage collector knows automatically when it can delete them. Additionally, access to local variables is much faster than to globals because Hollywood does not need to traverse through the whole global enviroment and finally, they increase the readability of your program.
Local variables have a limited lifetime. They will only be available in the block where you have declared them. A block will usually be the body of a function but it can also be the body a control structure. You can even declare blocks by using the Block and EndBlock statements.
Important: Local variables have to be declared explicitly. If you do not do this, the variable will be automatically global. For example, if you write
Function p_Add(a, b) tmp = a + b Return(tmp) EndFunction |
the variable tmp
will automatically be created as a global variable. But
this is just a waste of resources because you only need the variable inside
the function so you should better write:
Function p_Add(a, b) Local tmp = a + b Return(tmp) EndFunction |
Now the variable tmp
is explicitly declared local and will be deleted when
the function p_Add()
exits.
As you have already seen now, local variables are declared by using the Local statement. To declare a local variable, simply place the identifier Local before the declaration:
a = 10 ; global variable Local b = 10 ; local variable |
If you want to initialize multiple variables with one Local statement, simply uses commas as you would do in a normal assignment:
a, b = 10, 5 ; global variables! a receives 10, b receives 5 Local x, y = 10, 5 ; local variables! x receives 10, y receives 5 |
Once you have declared a local variable, you do not need to use the Local statement any longer:
; if x > 10, multiply it by 2, else divide it by 2 If x > 10 Local tmp = x ; declare local variable "tmp" tmp = tmp * 2 ; multiply local variable "tmp" by 2 x = tmp ; assign value of local "tmp" to "x" Else Local tmp = x ; declare local variable "tmp" tmp = tmp / 2 ; divide local variable "tmp" by 2 x = tmp ; assign value of local "tmp" to "x" EndIf Print(tmp) ; this will print 0 because "tmp" is gone |
The code above creates the local variable tmp
in the two blocks of the If
statement. After that it multiplies or divides it by 2. The identifier Local
is no longer required there because Hollywood already knows at this point
that tmp
is a local variable. tmp
will be deleted at the end of the block
so it is not available in the line Print(tmp)
any more. tmp
becomes Nil
after the block ends.
If you do not assign a value to the variable, it will get the value Nil but Hollywood will know that it is a local variable, e.g.:
If True ; block is always entered Local x ; declare local variable x Print(x) ; prints 0 because x is Nil x = 5 ; assign 5 to local x Print(x) ; prints 5 now EndIf ; scope of x ends here Print(x) ; prints 0 because x is Nil now again |
You can also use the name of a global variable (or a local variable of the superior block) for a new local variable. For instance:
a = 50 Block ; delimit the next two lines Local a = 40 ; create local "a" and assign 40 NPrint(a) ; prints 40 EndBlock ; scope of "a" ends here NPrint(a) ; prints 50 |
A more complex example which uses many variables with the same name follows:
x = 10 ; global x (x1 = 10) Block ; open new block Local x = x + 1 ; assign 11 to local x (x2 = x1 + 1) Block ; open new block Local x = x + 1 ; assign 12 to local x (x3 = x2 + 1) Block ; open new block Local x = x + 1 ; assign 13 to local x (x4 = x3 + 1) NPrint(x) ; prints 13 (= x4) EndBlock ; scope of x4 ends here NPrint(x) ; prints 12 (= x3) EndBlock ; scope of x3 ends here NPrint(x) ; prints 11 (= x2) EndBlock ; scope of x2 ends here NPrint(x) ; prints 10 (= x1) |
This code might look a bit confusing but it makes perfect sense. In every
new block Hollywood will look up the variable x
starting from the current
scope and traversing through all superior blocks.
It should be noted that you cannot use local variables together with Gosub() because Hollywood will jump out of the current block on a Gosub() and return to it later with totally different data on the stack. Thus, the following code will not work:
; invalid code Block Local a = 50 ; create local "a" Gosub(SUBROUTINE) ; jump out of the block; "a" will be trashed Print(a) ; local "a" is some random stack value now EndBlock |
This shouldn't be much of a problem because Gosub() is deprecated anyway and shouldn't be used in your code.
You can also use local functions. They work in almost the same way and are also preferable to global functions although you will most likely do not use them as excessively as you use local variables. But they can be handy from time to time. See Local functions for details.