Page 36 of 229 Start Back Next End ZBasic Language Reference 26 ZBasic Microcontrollers 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. The last statement of the function, just before the End Function, illustrates how the value to be returned by the function is set. It is useful to think of there being a variable automatically defined that has the same name as the function as well as the same type. We call this a return value variable. Within the function, you can refer to the return value variable, in most cases, just as you would any other variable or parameter. In the example above, instead of introducing a new variable factVal, we could have instead simply used the reference to the return value variable Factorial. The function’s code is re-written below using this idea. Function Factorial(ByVal val as Long) As Long Factorial = 1 Do While (val > 1) Factorial = Factorial * val val = val - 1 Loop End Function As with subroutines, variables and constants may be defined within a function. When this is done, the variable or constant is private to the function and cannot be directly accessed from any other routine. Although it is common to place such definitions near the beginning of the function, the definitions may occur anywhere in the subroutine as long as they occur before their first use. Note that the normal execution sequence of a function may be altered by using the Exit Function statement. When executed, this statement causes control to return to the caller immediately. Also, it is important to know that the return value variable of a function is not automatically initialized. If your function returns without having assigned a value to the return value variable, the return value will have an undefined value. Once a function is defined, it may be used anywhere a variable may be used, e.g. in an expression. One exception is that a function name may not be used on the left hand side of an assignment. Dim lval as Long lval = Factorial(4) If a function is defined as taking zero parameters, it may be invoked by giving its name without the parentheses following it. This form is supported for compatibility reasons but its use is discouraged. If the parentheses are present a reader of the code knows immediately that it is a function invocation as opposed to the use of a variable or constant.