Available on: all targets
See also: DIM (variable)

DIM (array)

The DIM command is used to reserve a specific space in the computer's memory to hold data that you will use during the execution of your program. In practice, it is like reserving empty boxes where you can store objects (data) of different sizes and types.

The DIM command is used primarily to create arrays, which are data structures that allow you to group multiple values under a single name, accessing each value through a numeric index. You can define both vectors (mono dimensional array) or a multidimensional array of values, and to initialize this array with the preferred values. You must also specify the size of the array, that is, the number of elements it can contain. When you declare an array with DIM, you reserve a portion of memory sufficient to hold all the elements of the array.

The simplest syntax is that which defines a vector of a single dimension: in this case, it is sufficient to indicate the number of elements in parentheses. With the keyword AS you can indicate the data type of each single element.

You can, of course, define a matrix (i.e. a vector with two or more dimensions). In this case it is sufficient to indicate the number of elements for each dimension, separating them with a comma.

By definition, the array will be initialized with a value of zero for each element. You can change this behavior by specifying an initialization by assignment. The initialization can be done by indicating a single initialization value, each single value of each single element, with the same type of data with which the matrix was created (with the # {...} syntax), or by describing the memory area that will occupy the array, byte by byte (with the # [...] syntax ).

The array could be assigned to a "read only" memory, in order to save RAM space. This can be done using READ ONLY flag.

Finally, the array can be assigned to a specific expansion bank by using the BANKED keyword. Otherwise, if an array could be used as destination of BANK READ / BANK WRITE command, you should add this flag.

Arrays allow you to organize data in a structured way, making it easier to access and manipulate. By declaring arrays before using them, you define a specific size of the data. The DIM command makes code more readable, because it explicitly declares the data structures used.

SYNTAX

 DIM name [AS type] (d1[,d2[,...]]) [ro] [fl]
 DIM name [AS type] (d1[,d2[,...]]) = #"["hex"]" [ro] [fl]
 DIM name [AS type] (d1[,d2[,...]]) = #{v,v,...} [ro] [fl]
 DIM name [AS type] WITH v (d1[,d2[,...]]) [ro] [fl]
   ro: [READ ONLY|READONLY]
   fl: [BANKED|BANKED(n)|UNBANKED|FOR BANK READ|FOR BANK WRITE]


Legend
  • id : identifier
  • type : datatype
  • v : value
  • "..." : string
  • [...] : optional

EXAMPLE

 DIM x(42)
 DIM values AS DWORD(20,20) BANKED
 DIM y AS BYTE(8) = #[ff80ff80ff80ff80]
 DIM z AS BYTE(8) = _
              #{ 255, 128, 255, 128, 255, 128, 255, 128 }
 DIM scores WITH 0 (2)
 DIM x(42)
 DIM y(4) AS BYTE = #{ 1, 2, 3, 4 }


Used in:

ABBREVIATION: Di

Join BASIC 10Liner Contest with ugBASIC!

An interesting competition is held at the beginning of each year: the BASIC 10Liner Contest. It is possible to use ugBASIC to participate in the next "BASIC10Liner" competition, in the following categories:

  • PUR-120 - A game in 10 lines of max 120 characters (w/abbrev.)
  • EXTREME-256 - A game in 10 lines of max 256 characters (w/abbrev.)
  • SCHAU - Any program in 10 lines of max 256 characters (w/abbrev.)
In order to reduce space you can use this abbreviation for this instruction:

DIM (array) ↔ Di

Any problem?

If you have found a problem with this keyword, if you think there is a bug or, more simply, you would like it to be improved, open an issue for this example on GitHub.
Thank you!

open an issue BACK TO KEYWORDS