BASIC stamp manual v2.2

BASIC Stamp Architecture – Defining Arrays
Page 88 BASIC Stamp Syntax and Reference Manual 2.2 www.parallax.com
myBytes VAR Byte(10) ' Define 10-byte array
idx VAR Nib ' Define 4-bit var
FOR idx = 0 TO 9 ' Repeat with idx = 0, 1, 2...9
myBytes(idx) = idx * 13 ' Write idx * 13 to each cell
NEXT
FOR idx = 0 TO 9 ' Repeat with idx = 0, 1, 2...9
DEBUG ? myBytes(idx) ' Show contents of each cell
NEXT
STOP
If you run this program, DEBUG will display each of the 10 values stored
in the elements of the array: myBytes(0) = 0*13 = 0, myBytes(1) = 1*13 = 13,
myBytes(2) = 2*13 = 26 ... myBytes(9) = 9*13 = 117.
A word of caution about arrays: If you’re familiar with other BASICs and
have used their arrays, you have probably run into the “subscript out of
range” error. Subscript is another term for the index value. It is
out-of-range when it exceeds the maximum value for the size of the array.
For instance, in the example above, myBytes is a 10-cell array. Allowable
index numbers are 0 through 9. If your program exceeds this range,
PBASIC will not respond with an error message. Instead, it will access the
next RAM location past the end of the array. If you are not careful about
this, it can cause all sorts of bugs.
If accessing an out-of-range location is bad, why does PBASIC allow it?
Unlike a desktop computer, the BASIC Stamp doesn’t always have a
display device connected to it for displaying error messages. So it just
continues the best way it knows how. It’s up to the programmer (you!) to
prevent bugs. Clever programmers, can take advantage of this feature,
however, to perform tricky effects.
Another unique property of PBASIC arrays is this: You can refer to the 0th
cell of the array by using just the array’s name without an index value. For
example:
myBytes VAR Byte(10) ' Define 10-byte array
myBytes(0) = 17 ' Store 17 to 0th cell
DEBUG ? myBytes(0) ' Display contents of 0th cell
DEBUG ? myBytes ' Also displays 0th cell
All
2