DIM

From C64-Wiki
Jump to navigationJump to search
BASIC keyword
Keyword: DIM
Abbreviation: D Shift+I
Type: Command
Token code: 134/$86
Handling routine
in BASIC ROM:
45185–45194
$B081–B08A
List of all BASIC keywords


Remark: This article describes the BASIC command DIM in BASIC V2 at the Commodore 64.

Type: Command
General Programming-Syntax: DIM <Variable>(<d1>[,<d2>[,<d3>...]])[, <Variable>(<d1>[,<d2>[,<d3>...]])...]

The BASIC command DIM allocates space in array memory for a new array, with one dimension for each of the dimension sizes d1, d2, d3, etc. given in the DIM statement.

Note that the dimension sizes represent the last, or "highest", index number available. Since the lower limit for the indices is always fixed at zero, specifying a dimension of n reserves space for n+1 variables "along" that dimension.

Omitting the list of dimension sizes causes a ?SYNTAX ERROR IN line. Previously DIMensioned arrays cannot be "re-dimensioned" through a second DIM command; this will raise a ?REDIM'D ARRAY ERROR IN line.

Note that using an array without DIMensioning is actually legal in Commodore BASIC: If a BASIC program e.g. starts assigning values to an array not "declared" in a prior DIM statement, the interpreter "auto-DIMensions" the array to a size of 10+1=11 elements along each specified dimension. Like other arrays, such "auto-DIMensioned" arrays cannot be re-dimensioned later through an explicit DIM command.

Indices to arrays are handled internally by signed positive 16-bit integers, which in theory gives them a range from 0 to 32767, resulting in up to 32768 variables along that dimension. Indices outside this range will raise an ?ILLEGAL QUANTITY ERROR. The number of dimensions are counted in a single-byte register, and thus has a limit at 255 dimensions. But memory limitations will cause an ?OUT OF MEMORY ERROR IN line long before an array attains such sizes.

Examples[edit | edit source]

DIM A$(4)      One-dimensional array with room for 4+1=5 strings.
DIM C%(2,3)    Two-dimensional array with 2+1=3 integers along the first dimension,
               and 3+1=4 dimensions along the second, giving a total of 12 integers.
DIM T(8,1,5)   Three-dimensional array with (8+1)×(1+1)×(5+1)=108 real variables.
DIM B$(CT%)    DIMensioning a string array according to a size held in a variable
Q(10)=10 
Bad coding, but legal - automatic DIMensioning, whereas Q(10) has got the value 10 and Q(0) until Q(9) the value 0 !

The following example defines an array of ten numbers (lines 10-20, line 20 is for having the size of the array in the variable N). Then asks the user for the values of the array (lines 40-115). After that it prints the contents of the array (lines 80-115). Then there is a loop for searching for values until a -1 is input by the user (lines 120-170). Notice the spaghetti code by having a jump to 120 at 140 for breaking the for loop.

10 dim a(10)
20 n=10
30 print "enter elements of the array"
40 for i=1 to n
50 print "a(";i;")";
60 input a(i)
70 next i
80 print "the elements are"
90 for i=1 to n
100 print a(i),
110 next i
115 print
120 input "enter an element to search for (-1 to quit)"; e
125 if e=-1 then end
130 for i=1 to n
140 if a(i)=e then print "found it at index"; i : goto 120
150 next i
160 print "could not find it"
170 goto 120


BASIC V2.0 (second release) Commands

ABS | AND | ASC | ATN | CHR$ | CLOSE | CLR | CMD | CONT | COS | DATA | DEF | DIM | END | EXP | FN | FOR | FRE | GET | GET# | GOSUB | GOTO | IF | INPUT | INPUT# | INT | LEFT$ | LEN | LET | LIST | LOAD | LOG | MID$ | NEW | NEXT | NOT | ON | OPEN | OR | PEEK | π | POKE | POS | PRINT | PRINT# | READ | REM | RESTORE | RETURN | RIGHT$ | RND | RUN | SAVE | SGN | SIN | SPC | SQR | STATUS/ST | STEP | STOP | STR$ | SYS | TAB | TAN | THEN | TIME/TI | TIME$/TI$ | TO | USR | VAL | VERIFY | WAIT