Start Back Next End
  
ZBasic Language Reference
22
ZBasic Microcontrollers
BasicX Compatibility Note
In BasicX mode, all variables and constants must be defined
before the first subroutine or function in a module.
Each of these program items may be defined to be Public or Private.  A public item is visible to other
modules and may be referenced in the definitions contained in other modules.  A private item is visible
only within the module in which it is defined.  Generally speaking, unless there is a specific need for an
item to be public, it should be private.  If you make something private and later decide that you need to
reference it in another module, it is a simple matter to change the definition from private to public.
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
Previous page Top Next page