57 of 208
ZBasic Language Reference 49 ZX Microcontroller Family Dim count as Integer = 5 Dim str as String = "column" 2.13 Type Conversions The ZBasic language is strongly typed meaning, for example, that it is not allowed to assign the value of a constant, variable or parameter of one type to a variable or parameter of a different type. There are two apparent exceptions to the strong-type regimen. The first exception is with respect to integral numeric literals. An integral literal is considered to have a universal integral type (32-bit internally) so it can be assigned to a parameter or variable of any integral type (Byte, Integer, UnsignedInteger, etc.). Note that the presence of a plus sign or minus sign on a numeric literal does not change this interpretation so it is allowable to assign the value –¹ to an unsigned variable type. The second apparent exception to the strong typing rules occurs with the System Library routines. Many of these routines will accept two or more data types for some of their parameters. It is as though several different versions of the library routines exist, differing only in the types of the parameters that they accept. This computer science concept is known as polymorphism. The System Library routines include a set of functions for performing type conversions. The first set, CBool(),CByte(), CInt(), CUInt(), CLng(), CULng(), CSng() and CStr() allows, with some exceptions, conversion of a value of an arbitrary type to the target type. The second set, FixB(), FixI(), FixUI(), FixL(), and FixUL() are specifically for converting Single values to the target type. The difference between using FixI() and CInt(), for example, to convert a Single value is the rounding method used. The final set of conversion functions, CType() and To<enum>() is for converting an integral value to an enumeration member. See the ZBasic System Library Reference Manual for more details on these conversion functions. 2.14 Parameter Passing Conventions When a subroutine or function is defined, part of the definition specifies the parameters and their types that are expected by the routine. These parameters are referred to as the “formal parameters”. When a subroutine or function is invoked, parameters must be provided that match the formal parameters in order, number and type. These parameters are referred to as the “actual parameters” for each invocation. As discussed earlier, parameters may be passed to subroutines and functions either “by value” or “by reference”. The differences between these two methods are subtle but important. When passed by value, the actual parameter may be an individual value like a constant or variable or it may be an expression. In either case the value of the actual parameter is calculated and the resulting value is passed to the routine. Within the called routine, the passed value may be utilized in any manner; it may even be modified and this modification will have no effect on any of the constituent elements of the passed parameter value. Effectively, the called routine gets its own private copy of the passed value. When passed by reference, the address of the actual parameter is passed to the called routine. This implies that the actual parameter must be an individual variable or another parameter and not an expression nor a constant. Since the called routine has the address of the actual parameter, it is able to both read from and write to the actual parameter unless otherwise restricted. The ability of a routine to modify a variable passed by reference is often useful, especially in cases where a routine needs to produce multiple values for use by the caller. On the other hand, if used carelessly, it can be the source of errant program operation that is difficult to diagnose. There are restrictions on whether a particular variable type may, may not, or must be passed by value or by reference. For example, a persistent variable cannot be passed by reference because the called routine is expecting the address of a RAM-based variable. Also, for efficiency reasons some variables are always passed to routines by providing the variable address, even if the definition of the routine specifies the parameter is to be passed by value. In such cases, the compiler treats the parameter as being read-only, effectively enforcing the semantics of pass-by-value. Any attempt to modify a read-only