Functions can be used to break down your program into several smaller code sections which increases the readability and structure of your code. A function can be regarded as a little program of its own. It can use variables that are local to the function, which means that they will only be available inside the function and cannot be accessed from the outside. Of course, you can also access global variables from a function. Synonyms for the term "function" are the terms "procedure", "subroutine" or "statement". Functions can return nothing or any number of values of any type.
You can declare your own functions by using the identifiers
Function p_Add(a, b) Return(a + b) EndFunction c = p_Add(5, 2) ; c receives the value 7
You should always use the prefix
p_ in your own function names. This helps
to distinguish between your own functions and built-in Hollywood functions.
Also, in future versions of Hollywood there might be functions that have the
same name as functions in your code. This could lead to unexpected results.
So you should always use
p_ in your function names so that there will not
be any confusion. The
p_ stands for "private function".
Functions have to be declared before they are called, so the following code will give you an error:
c = p_Add(5, 2) Function p_Add(a, b) Return(a + b) EndFunction
Hollywood will try to call the function
p_Add() but will not find it because
it has not been declared yet. The two variables
b will be local to
the function's scope which means that you can only access them inside the
p_Add() function. If you pass more arguments to the function than it expects,
c = p_Add(5, 2, 4) ; c receives the value 7
then all superfluous arguments will be discarded. In this case the argument number 3 will be thrown away. If you pass less arguments to the function than it expects, for instance
c = p_Add(5) ; c receives the value 5 because 5 + Nil = 5
then Hollywood will pass the special value Nil for all arguments, which the function expects but which were not specified.
Functions can return values by using the Return() statement. It is obligatory that the return values are enclosed by parentheses. If you need to return multiple values, simply separate them by using commas. For example:
Function p_SomeValues() Return(5, 6, 7, 8, 9, 10) EndFunction
When you call functions which return multiple arguments and you do not specify enough variables to hold all the return values, all return values you did not specify variables for will be discarded, e.g.:
a, b, c = p_SomeValues()
The line above assigns 5 to
a, 6 to
b and 7 to
c. The return values
8, 9 and 10 will be discarded. If you specify more variables than there are
return values, the superfluous variables will get the special value Nil:
a, b, c, d, e, f, g, h = p_SomeValues() ; "g" and "h" will be Nil
This line has two superfluous variables
h. They will be assigned
the value Nil because the
p_SomeValues() function returns only six values.
Of course, you can also define functions which do not return any value. These functions are also called statements. For example:
Function p_WaitSecs(s) Wait(s, #SECONDS) EndFunction
If you try to get a return value from statements, you will just receive Nil as shown in the following line:
a = p_WaitSecs(5)
a will be set to Nil because
p_WaitSecs() does not return any