Variable Declarations


Declaring Variables in a Script


In order to use variables they must be declared somewhere in the body of your script before you attempt to assign to them or reference them in any expression.


The syntax for declaring simple types variables is as follows:

type identifier [ = intializer ]


Where: type is NUMBER or STRING.

identifier is the variable name.

initializer is an optional numeric or string expression


Simple type declarations are very straightforward, for example:


NUMBER i = 1


This creates a variable of type Number and initializes it to a value of 1.

Note: When a variable of either type is declared with no initializer, it will be initialized to 0 for numbers or the empty string "" for strings.


A slightly different example:


Number A = 5, b = 10, c = a + b


This is legal because by the time the interpreter gets to the declaration for c, a and b have been created and assigned the value of their constant initializers.

In this case c will be set equal to 15.


NOTE: It is not legal to use the return value of a function as an initializer.

For instance: NUMBER i = ASC(CHR(10)) will not work, this is due to the internal structure of the scripting language and must be avoided.


Declaring Arrays


The syntax for declaring arrays of either type is as follows:

type identifier( Dim 0 [,Dim 1, ... ,Dim 5])


Where: type is NUMBER or STRING.

identifier is the variable name.

The () denote that this will be an array variable.

Dim n is the dimension of the array. You can declare arrays of up to five dimensions maximum.


Restrictions

Though there is also no "hard" limit on array dimensions, there are resource limits.

If sufficient system resources are not available to create an array, a runtime error will occur and the array will not be created.


Initializers are NOT legal in array declarations.


Examples of Array declarations


NUMBER a(20) ' create a 21 element array - elements numbered 0 - 20

STRING b(20) ' create a 21 element array - elements numbered 0 - 20

NUMBER i, x = 10


' initialize the arrays with some value.

FOR i = 0 TO 20

a( i ) = x

b( i ) = STR(x)

x = x + 10

NEXT i


FOR i = 0 to 20

PRINT "Element ";i;" of a() is ";a( i )

PRINT "Element ";i;" of b() is ";b( i )

NEXT

ERASE a ' Sets all elements to 0

ERASE b ' Sets all elements to ""