Page 31 of 229 Start Back Next End ZBasic Language Reference 21 ZBasic Microcontrollers Defining Constants It is often convenient to define constants that can be used in other parts of the program. Doing so generally helps clarify the purpose of the value, assuming a reasonably descriptive name is chosen, and also facilitates easier maintenance and modification of the program. The syntax for defining a constant is as follows: [Public | Private] Const <name> As <type> = <value> If neither Public nor Private is specified, the constant will be private. The <name> must be a legal identifier as described in Section 2.1. The <type> must be one of the fundamental data type names described in Section 2.2 or an Enum type (described in Section 3.2). Lastly, the <value> element must be value or an expression that has a constant value and is the same type as (or compatible with) the specified <type>. In many cases, the <value> will be a simple numeric literal like -55 or 3.14159. In the case of string constants, it may be a literal string like "Hello, world!". However, it is sometimes convenient to define a constant in terms of another constant. Consider the example below. Private Const Pi as Single = 3.14159 Const TwoPi as Single = Pi * 2.0 You may also use certain System Library functions in the constant’s value expression. The restriction is that the expression must be able to be evaluated at compile time. Private Const Pi as Single = ACos(-1.0) Private Const InitialValue as Single = Sin(Pi / 2.0) The definition of the value of pi in the manner shown in the first example is useful because it results in the maximum accuracy of the constant value. String constants are sometimes useful as well. Public Const VersionNum as String = "V1.0" Public Const VersionDate as String = "Oct 2005" Public Const VersionStr as String = VersionNum & " " & VersionDate Public Const VersionDateUC as String = UCase(VersionDate) You may define multiple constants of the same or different types on the same line. Const c1 as Integer = 7, c2 as Single = 3e10 BasicX Compatibility Note In BasicX mode, UnsignedInteger, UnsignedLong and String constants are not supported. Also, constant expressions cannot utilize built-in functions. Defining Variables To define a variable at the module level (as opposed to within a subroutine or function, described later) the syntax is: {Public | Private | Dim} <name> As <type>