Page 35 of 229 Start Back Next End ZBasic Language Reference 25 ZBasic Microcontrollers Call Blink(redLed, 0.5) It is recommended that you invoke subroutines as shown using the Call keyword as shown. However, for compatibility with other Basic dialects it is also possible, although not recommended, to invoke a subroutine by using its name as if it were a statement. If this is done, the actual parameters are not allowed to have enclosing parentheses as illustrated below. Blink redLed, 0.5 One powerful aspect of subroutines is that variables and constants may be defined within a subroutine itself. When this is done, the variable or constant is private to the subroutine and cannot be directly accessed from any other routine. Although it is common to place such definitions near the beginning of the subroutine, 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 subroutine may be altered by using the Exit Sub statement. When this statement is executed, it causes control to return to the caller immediately bypassing the remaining statements in the subroutine. 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