Because functions in Hollywood are just variables of type "function", you can also use local functions which have a limited lifetime. They work pretty much the same way than local variables and have the same advantages too. Here is an example of a local function:
Block Local p_Add = Function(a, b) Return(a + b) EndFunction Print(p_Add(5, 6)) ; prints 11 EndBlock
In the above code, the function
p_Add() will be local to the block it has
been declared in. Thus, any attempts to call
p_Add() after the EndBlock
statement will lead to an error.
You could also use the more common function definition to create local functions. This code does the same as the code above but uses the common way of declaring functions:
Block Local Function p_Add(a, b) Return(a + b) EndFunction Print(p_Add(5, 6)) ; prints 11 EndBlock
Using local functions can also become handy if you want to temporarily replace a Hollywood function. For example, the following code replaces the DebugPrint() function with the Print() function but only for the lifetime of the block where it has been defined (and in subordinate blocks):
If error = True Local Function DebugPrint(...) Print(Unpack(arg)) EndFunction DebugPrint("An error occurred!") ; redirects to Print() EndIf DebugPrint("Hello") ; points to DebugPrint() again
The string "An error occurred!" will be rendered to your display in the code above because we have defined a local function called DebugPrint() which calls the Hollywood function Print(). This local DebugPrint() will be killed when the block is left. The following call to DebugPrint() will then call the real Hollywood DebugPrint() function.