Page 19 of 62
SBasic User's Manual SBasic Version 2.7 Page 19
Printed: December 5, 1999
Variables, arrays, and named constants
SBasic requires you to declare the names of all variables used in your
program. You declare variables with the DECLARE command. For
example,
declare foo
creates the SBasic variable FOO.
Variable names must begin with an alphabetic character or an
underscore ('_'); remaining characters in a variable name can also
include digits.
NOTE: Though legal, starting variable names with an underscore can
cause obscure problems if you embed assembly language in your SBasic
source file. See the section below on ASM and ENDASM, regarding
references to SBasic variables from within an ASM block.
All variables use two bytes of RAM. The first variable defined is
always located at assembler address VARBEG. Variables are assigned
addresses based on the order of their declarations.
You must declare a variable before your code can reference that
variable. This means that you will usually place all DECLARE
statements in a block at the beginning of your SBasic source file.
Note that, unlike traditional Basics, SBasic does not automatically
initialize all variables to zero. The value of any variable following
system reset is unknown! Your SBasic program must provide any needed
variable initialization.
SBasic also supports single-dimension arrays, or vectors. Each
element in an array occupies one 16-bit location (two bytes). You use
the DECLARE statement to define an array in much the same way you use
it to define a simple variable. For example:
declare foo(5)
defines the array FOO, consisting of five sequential 16-bit locations.
The first element in any array is always element zero. Thus, FOO in
the above example consists of the five elements named FOO(0) through
FOO(4).
You can use arrays anywhere a variable name would be legal, including
the left side of an assignment operator. For example:
declare foo(5)
foo(2) = 100/n