FreeBasic Arrays
Last modified October 18, 2026
An array holds several values of one type under a single name. This article covers fixed-size arrays, multi-dimensional arrays, dynamic arrays that change size at run time, and a sorting example that passes an array to a procedure. See the keyword pages Dim, Redim, Preserve, and LBound and UBound for the details.
Fixed-size arrays
A fixed-size array is declared with Dim and explicit bounds, or with a single number
that gives the upper bound. The bounds decide how many elements the array holds, and the storage
is reserved for as long as the array exists. The example also shows a two-dimensional array,
which is indexed by row and column.
Dim primes(0 To 4) As Integer = {2, 3, 5, 7, 11}
Dim i As Integer
For i = LBound(primes) To UBound(primes)
Print "primes("; i; ") = "; primes(i)
Next
Dim grid(1 To 2, 1 To 3) As Integer
grid(1, 2) = 42
Print "grid(1,2) = "; grid(1, 2); " size: "; UBound(grid, 1); " x "; UBound(grid, 2)
Dim primes(0 To 4) creates five elements, numbered 0 to 4. The bounds are part of the
declaration. The initializer list fills the elements in order. LBound and
UBound return the bounds, so the loop does not depend on hard-coded numbers.
The two-dimensional array grid(1 To 2, 1 To 3) has two rows and three columns; the
element grid(1, 2) is row 1, column 2. UBound(grid, 1) returns the upper
bound of the first dimension.
$ fbc -lang fb arr_static.bas $ ./arr_static primes( 0) = 2 primes( 1) = 3 primes( 2) = 5 primes( 3) = 7 primes( 4) = 11 grid(1,2) = 42 size: 2 x 3
Dynamic arrays
Dim values() As Integer declares an array with no size. ReDim allocates
it with a chosen size at run time. A later ReDim discards the old contents unless
Preserve is used.
Dim values() As Integer ReDim values(1 To 3) values(1) = 10 values(2) = 20 values(3) = 30 Print "Before: "; UBound(values); " elements" ReDim Preserve values(1 To 5) Print "After Preserve: "; UBound(values); " elements, values(2) = "; values(2) Erase values Print "After Erase, UBound = "; UBound(values)
After the first ReDim, the array has three elements. ReDim Preserve
enlarges it to five elements and keeps the values already stored, so values(2) is
still 20. Erase releases the array, and UBound then returns -1, which
means the array is empty.
$ fbc -lang fb arr_dynamic.bas $ ./arr_dynamic Before: 3 elements After Preserve: 5 elements, values(2) = 20 After Erase, UBound = -1
Sorting an array with a procedure
Bubble sort compares neighboring elements and swaps them when they are out of order. Each pass
moves the largest remaining value to the end, so the inner loop shrinks as the outer loop runs.
The array is passed to BubbleSort as an open array parameter, so the sort works on the
caller's array.
Sub BubbleSort(a() As Integer)
Dim i As Integer
Dim j As Integer
Dim tmp As Integer
For i = LBound(a) To UBound(a) - 1
For j = LBound(a) To UBound(a) - 1 - (i - LBound(a))
If a(j) > a(j + 1) Then
tmp = a(j)
a(j) = a(j + 1)
a(j + 1) = tmp
End If
Next
Next
End Sub
Dim values(0 To 5) As Integer = {42, 7, 19, 3, 88, 15}
BubbleSort(values())
Dim k As Integer
For k = 0 To UBound(values)
Print values(k);
Next
Print
The outer loop runs once per pass. The inner loop compares a(j) with
a(j + 1) and swaps them when they are in the wrong order. The upper limit of the inner
loop decreases by one on each pass, because the largest values have already reached the end.
After sorting, values holds the numbers in ascending order.
$ fbc -lang fb arr_sort.bas $ ./arr_sort 3 7 15 19 42 88
Source
Author
List all FreeBasic Tutorials.