12.2 Functions are variables

In Hollywood functions are just variables of the type function. Therefore, you can easily assign them to other variables, e.g.:

 
myfunc = DisplayBrush         ; assign DisplayBrush to "myfunc"
myfunc(1, #CENTER, #CENTER)   ; calls DisplayBrush(1, #CENTER, #CENTER)

You can even write the definition of a function as an assignment:

 
p_Add = Function(a, b) Return(a + b) EndFunction
c = p_Add(5, 2)   ; c receives 7

The definition of p_Add() in the first line is the same as if you wrote:

 
Function p_Add(a, b)
    Return(a + b)
EndFunction

You could also replace Hollywood functions with your own ones, e.g. if you want all Print() calls to use DebugPrint() instead, the following code could do this:

 
Function p_Print(...)
    DebugPrint(Unpack(arg))   ; redirect arguments to DebugPrint()
EndFunction
Print = p_Print        ; all calls to Print() will call p_Print() now
Print("Hello World!")  ; Print() refers to p_Print() now

Or an even simpler solution:

 
Print = DebugPrint    ; redirect all calls to Print() to DebugPrint()
Print("Hello World!") ; calls DebugPrint() directly


Show TOC