27 of 158
21 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. BasicX Compatibility Note In BasicX compatibility mode, when a function is defined as returning an UnsignedInteger or UnsignedLong type, the very first line of the function must be a Set statement 2.4 Expressions Expressions are an important part of most ZBasic programs. They provide the means by which your programs implement the mathematical, logical and comparison operations necessary for your application. Generally, anywhere a value may be used, an expression may be used as well. An expression consists of one or more values, called operands, and one or more operators that indicate the function to perform on the operands. Example b = b * 5 + 3 In this assignment statement, the value being assigned to the variable b is an expression comprising three operands and two operators. Some operators, like both of those in the expression above, are called “binary operators” because they require two operands. Other operators require only one operand and are called “unary operators”. The available operators and their characteristics are described in subsequent sections. ZBasic is a strongly typed language. This means that operands supplied for binary operators must generally be of the same type. The only exception to this rule is the exponentiation operator which allows a restricted mixing of types as described in Section 2.4.3. The order of evaluation of the components of an expression is governed by operator precedence and associativity as described in the next two sections. However, that order may be overridden by the use of parentheses. 2.4.1 Operator Precedence Consider again the example expression b = b * 5 + 3. Each of the operators requires two operands but it may not be immediately clear what the operands are in each case. It could be that the expression above means to add 5 to 3 and then multiply the result by the value of b. On the other hand, it could mean to multiply the value of b by 5 and then add 3 to the result. The property that dictates the order in which operators are applied in this case is called “operator precedence”. An operator having higher precedence has priority over an operator with lower precedence and is therefore applied first. The table below depicts the precedence of ZBasic operators.