31 of 208
ZBasic Language Reference 23 ZX Microcontroller Family Defining Functions A function is a collection of statements that can be executed by using the function name in place of a value or in an expression. The advantage of creating functions is that we can think of them as logical blocks instead of thinking about all of the details that are dealt with by the statements within the functions. Like a subroutine, a function may have zero or more parameters defined. If it has parameters defined, you must supply values for each of the parameters when you invoke the function. The syntax for defining a function is very similar to that for defining a subroutine and is shown below. [Public | Private] Function <name> ( [<parameter-list>] ) As <type> [<statements>] End Function The primary difference is the use of the keyword Function in place of Sub and the specification of a type for the value to be returned by the function. Like a subroutine, if neither Public nor Private is specified, Public is assumed. The <parameter-list> syntax is identical to that for a subroutine. Example ' compute the factorial of the value provided Public Function Factorial(ByVal val as Long) As Long Dim factVal as Long factVal = 1 Do While (val > 1) factVal = factVal * val val = val - 1 Loop Factorial = factVal End Function This function implements a mathematical operation called factorial. The value of N factorial (usually written N!) is the product of all the integers from 1 up to and including N. The value of 0! is, by definition, 1. The value of 3! is 6 and so on. This example introduces a couple of statements that we haven’t seen yet but they are fairly straightforward. Notice that a variable named factVal was defined within the function. This variable is called a “local variable” and it is not visible to any code outside of the function. The other aspect of this local variable that is different from variables defined at the module level is that this variable only takes up space while the function is executing. When the function returns, the space used by the local variable is reclaimed by the system and can be used for other purposes. See Section 3.1 for more information on the concepts of scope and lifetime of variables. The second line of the function shows a value being assigned to the variable factVal. Instead of using the literal constant value as shown on the right hand side of the equal sign, we could just as well have written an expression that involved several values (perhaps including function invocations) and operators like addition, subtraction, multiplication, etc. This statement is known as an assignment statement and is described in more detail in Section 2.5.1. The third line of code illustrates a useful variation on the Do loop that we have already seen. In this case, the Do has a condition associated with it. The condition is tested before every pass through the loop and as long as the conditional expression evaluates to the Boolean value True, the statements within the loop are executed again. In this case, the condition tests if the val parameter has a value greater than 1, the statements in the loop will be executed. Otherwise, control will transfer to the first statement following the Loop statement. Within the loop, there are two more assignment statements, each of which has an expression on the right hand side, the first involving multiplication and the second involving subtraction. The meaning of the expressions should be self-evident.