====== Constants ======
Constants can be used to declare a constant value (a value that cannot be
changed) within the program. This essentially allows a number to be
assigned a name, often improving readability and debugging of the source
code.
===== Const =====
The ''Const'' statement can be used to declare constants in Cubloc BASIC:
Const PI As Single = 3.14159
Const WRTTIME As Byte = 10
Const MSG1 As String = "ACCESS PORT"
When the constant is not given a type, the compiler will find an appropriate
type for it as shown below:
Const PI = 3.14159 ' Declare as SINGLE
Const WRTTIME = 10 ' Declare as Byte
Const MYROOM = 310 ' Declare as Integer since it’s over 255.
Const MSG1 = "ACCESS PORT" ' Declare as String
===== Con (Another CONST method) =====
The ''Con'' statement can be also used to declare constants as shown below:
PI Con 3.14159 ' Declare as Single.
WRTTIME Con 10 ' Declare as Byte
MYROOM Con 310 ' Declare as Integer
MSG1 Con "ACCESS PORT" ' Declare as String
===== Constant Arrays =====
With constant arrays, the user is able to store a list of values before the
program begins. A program requiring a large number of constant values
can be simplified as shown below:
Const Byte DATA1 = (31, 25, 102, 34, 1, 0, 0, 0, 0, 0, 65, 64, 34)
I = 0
A = DATA1(I) ' Store 31 in A.
I = I + 1
A = DATA1(I) ' Store 25 in A.
Const Byte DATA1 = ("CUBLOC SYSTEMS")
''String'' data can be stored in ''Byte'' constant arrays. Each ''Byte'' contains the
ASCII code of each character in the ''String''. In the example above, if ''DATA1(0)'' is read, the ASCII code of 'C' is returned. Likewise if ''DATA1(1)'' is read, ASCII code of 'U' is returned.
''Integer'' and floating point (''Single'') numbers can also be stored in arrays
as shown below:
Const Integer DATA1 = (6000, 3000, 65500, 0, 3200)
Const Long DATA2 = (12345678, 356789, 165500, 0, 0)
Const Single DATA3 = (3.14, 0.12345, 1.5443, 0.0, 32.0)
For multiple-line constant arrays, end each line with a comma, or an
underscore character as shown :
1)
Const Byte DATA1 = (31, 25, 102, 34, 1, 0, 0, 0, 0, 0, 65, 64, 34,
12, 123, 94, 200, 0, 123, 44, 39, 120, 239,
132, 13, 34, 20, 101, 123, 44, 39, 12, 39)
2)
Const Byte DATA2 = (31, 25, 102, 34, 1, 0, 65, 64, 34_
, 101, 123, 44, 39, 12, 39)
Please make note of the following differences between arrays and constant
arrays.
| ^ Array ^ Constant Array ^
^ Storage Location | Data Memory (SRAM) | Program Memory(FLASH) |
^ When Allocated | At Runtime | When Downloaded |
^ Can be Changed | Yes | No |
^ Purpose | Store changing values | Store constant values |
^ When Powered Off | Lost | Retained |
===== Demo =====
{{:cubloc:const:cublocconst.png?600|}}
===== String Constants =====
String constant arrays are deprecated.
See also [[:cubloc:define:|#define]].
[[cubloc:index#system_library:|Go CUBLOC home]]