DIM LOCAL STATIC

Compatible with:
DOS Maximite CMM MM150 MM170 MM+ MMX Picromite ArmiteL4 Armite F4 ArmiteH7 Picomite CMM2

Syntax:
DIM [type] decl [,decl]...
LOCAL variable [, variables]...
STATIC variable [, variables]...

Description:

DIM [type] decl [,decl]...
Declares one or more variables (ie, makes the variable name and its characteristics known to the interpreter).
When OPTION EXPLICIT is used (as recommended) the DIM, LOCAL or STATIC commands are the only way that a variable can be created. 
If this option is not used then using the DIM command is optional and if not used the variable will be created automatically when first referenced.
The type of the variable (ie, string, float or integer) can be specified in one of three ways:

By using a type suffix (ie, !, % or $ for float, integer or string). 
For example: DIM nbr%, amount!, name$

By using one of the keywords FLOAT, INTEGER or STRING immediately after the command DIM and before the variable(s) are listed. 
The specified type then applies to all variables listed (ie, it does not have to be repeated).
For example: DIM STRING first_name, last_name, city

By using the Microsoft convention of using the keyword "AS" and the type keyword (ie, FLOAT, INTEGER or STRING) after each variable. 
If you use this method the type must be specified for each variable and can be changed from variable to variable. 
For example: DIM amount AS FLOAT, name AS STRING

Floating point or integer variables will be set to zero when created and strings will be set to an empty string (ie, ""). 
You can initialise the value of the variable with something different by using an equals symbol (=) and an expression following the variable definition. 
For example: DIM STRING city = "Perth", house = "Brick"
The initialising value can be an expression (including other variables) and will be evaluated when the DIM command is executed.
See the chapter in the manual "Defining and Using Variables" for more examples of the syntax.

As well as declaring simple variables the DIM command will also declare arrayed variables (ie, an indexed variable with up to five dimensions on the CMM2). 
Note that this is different from the original Colour Maximite and Micromite versions of MMBasic which supported up to eight dimensions.
Following the variable's name the dimensions are specified by a list of numbers separated by commas and enclosed in brackets. 
For example: DIM array(10, 20)
Each number specifies the number of elements in each dimension. Normally the numbering of each dimension starts at 0 but the OPTION BASE
command can be used to change this to 1.
The above example specifies a two dimensional array with 11 elements (0 to 10) in the first dimension and 21 (0 to 20) in the second dimension. 
The total number of elements is 231 and because each floating point number on the Colour Maximite 2 requires 8 bytes a total of 1848 bytes of memory will
be allocated.
Strings will default to allocating 255 bytes (ie, characters) of memory for each element and this can quickly use up memory when defining arrays of strings. 
In that case the LENGTH keyword can be used to specify the amount of memory to be allocated to each element and therefore the maximum length of the string that can be stored. 
This allocation ('n') can be from 1 to 255 characters.
For example: DIM STRING s(5, 10) will declare a string array with 66 elements consuming 16,896 bytes of memory while: DIM STRING s(5, 10) LENGTH 20 will only consume 1,386 bytes of memory. 
Note that the amount of memory allocated for each element is n + 1 as the extra byte is used to track the actual length of the string stored in each element.
If a string longer than 'n' is assigned to an element of the array an error will be produced. 
Other than this, string arrays created with the LENGTH keyword act exactly the same as other string arrays. 
This keyword can also be used with non array string variables but it will not save any memory unless the length is less than 16 when it will both save memory and improve performance.
In the above example you can also use the Microsoft syntax of specifying the type after the length qualifier. 
For example: DIM s(5, 10) LENGTH 20 AS STRING
Arrays can also be initialised when they are declared by adding an equals symbol (=) followed by a bracketed list of values at the end of the declaration. 
For example: DIM INTEGER nbr(4) = (22, 44, 55, 66, 88)
or DIM s$(3) = ("foo", "boo", "doo", "zoo")

Note that the number of initialising values must match the number of elements in the array including the base value set by OPTION BASE. 
If a multi dimensioned array is initialised then the first dimension will be initialised first followed by the second, etc.
Also note that the initialising values must be after the LENGTH qualifier (if used) and after the type declaration (if used).

' Assigning a float to a DIMed integer variable is OK and the float is converted to integer.
' The internal conversion from float to Integer is equivalent to CINT(float)
 DIM c%
 c% =  2.4 : PRINT c% , CINT(2.4),  INT(2.4)
 c% =  2.4 : PRINT c% , CINT(2.6),  INT(2.6)
 c% = -2.4 : PRINT c% , CINT(-2.4), INT(-2.4)
 c% = -2.6 : PRINT c% , CINT(-2.6), INT(-2.6)
' Assigning a strict integer to a float variable is OK
 DIM e!
 e! = &hFFFF : PRINT e!
 e! = &hFFFFFFFFFFFFFFFF : PRINT e!
 

LOCAL variable [, variables]
Defines a list of variable names as local to the subroutine or function. 
This command uses exactly the same syntax as DIM and will create variables that will only be visible within the subroutine or function. 
They will be automatically discarded when the subroutine or function exits.

If a variable is declared as a parameter in a SUB or FUNCTION declaration, it should NOT be declared as LOCAL or STATIC and will produce an error if you do.


FUNCTION epochX(dt$) AS INTEGER
LOCAL dt$
'''
Error in line 16: DT already declared

STATIC variable [, variables]
Defines a list of variable names which are local to the subroutine or function. 
These variables will retain their value between calls to the subroutine or function (unlike variables created using the LOCAL command). 
This command uses exactly the same syntax as DIM. 
The only difference is that the length of the variable name created by STATIC and the length of the subroutine or function name added together cannot exceed 32 characters. 
Static variables can be initialised to a value. This initialisation will take effect only on the first call to the subroutine (not on subsequent calls).

Example showing the use of STATIC

print pseudo(1245) ' seed the random number generator
for n = 1 to 10
  print pseudo()
next n

function pseudo(s as integer) as float
static integer x=7
static integer a=1103515245
static integer c=12345
static integer m=2^31
if s <> 0 then x = s
x=(a * x + c) mod m
pseudo = x/m
end function

Last edited: 05 August, 2022