FreeBasic LBound and UBound Keywords
last modified June 16, 2025
The FreeBasic LBound
and UBound
keywords are used to
determine the lower and upper bounds of an array. These functions are essential
for working with arrays of unknown or variable sizes.
Basic Definition
LBound
returns the smallest valid index (lower bound) of an array
dimension. UBound
returns the largest valid index (upper bound) of
an array dimension.
Both functions take the array as their first parameter and an optional dimension number as the second parameter. For single-dimensional arrays, the dimension parameter can be omitted.
Basic Array Bounds
This example shows the simplest usage of LBound and UBound with a 1D array.
Dim numbers(1 To 5) As Integer Print "Lower bound: "; LBound(numbers) Print "Upper bound: "; UBound(numbers)
We declare an array with explicit bounds from 1 to 5. LBound returns 1 and UBound returns 5. These functions help make code more flexible when array bounds might change.
Default Array Bounds
FreeBasic arrays have default bounds when not explicitly specified.
Dim values(5) As Double Print "Default lower bound: "; LBound(values) Print "Default upper bound: "; UBound(values)
When bounds aren't specified, FreeBasic uses 0 as the default lower bound. This array has indices from 0 to 5. Always check bounds to avoid off-by-one errors in your code.
Multi-dimensional Arrays
LBound and UBound work with multi-dimensional arrays by specifying the dimension.
Dim matrix(1 To 3, 1 To 4) As Integer Print "First dimension bounds:" Print "LBound: "; LBound(matrix, 1) Print "UBound: "; UBound(matrix, 1) Print "Second dimension bounds:" Print "LBound: "; LBound(matrix, 2) Print "UBound: "; UBound(matrix, 2)
This 2D array has different bounds for each dimension. The second parameter specifies which dimension to check. Dimension numbering starts at 1 for the first dimension.
Dynamic Arrays
LBound and UBound are particularly useful with dynamic arrays whose size changes.
Dim dynamicArray() As String ReDim dynamicArray(5 To 10) Print "Initial bounds:" Print LBound(dynamicArray), UBound(dynamicArray) ReDim Preserve dynamicArray(5 To 15) Print "After resize:" Print LBound(dynamicArray), UBound(dynamicArray)
The array is initially dimensioned from 5 to 10, then resized to 5 to 15. LBound and UBound automatically reflect these changes. This prevents hardcoding array sizes in your code.
Iterating Through Arrays
Using LBound and UBound makes array iteration more robust and maintainable.
Dim fruits(3 To 7) As String fruits(3) = "Apple" fruits(4) = "Banana" fruits(5) = "Cherry" fruits(6) = "Date" fruits(7) = "Elderberry" For i As Integer = LBound(fruits) To UBound(fruits) Print fruits(i) Next
This loop works correctly regardless of the array's lower and upper bounds. If the array bounds change later, the loop will still work without modification. This is more reliable than hardcoded loop limits.
Function Parameters
LBound and UBound work with arrays passed to functions and procedures.
Sub PrintArrayBounds(arr() As Integer) Print "Array bounds in function:" Print LBound(arr), UBound(arr) End Sub Dim testArray(10 To 20) As Integer PrintArrayBounds(testArray)
The function correctly reports the bounds of the passed array. This allows functions to work with arrays of any bounds. The array bounds information is preserved when passed to functions.
Checking Empty Arrays
LBound and UBound can help detect uninitialized or empty arrays.
Dim emptyArray() As Integer On Error Goto ErrorHandler Print "Lower bound: "; LBound(emptyArray) Print "Upper bound: "; UBound(emptyArray) Exit Sub ErrorHandler: Print "Array is not dimensioned"
Attempting to get bounds of an undimensioned array causes an error. This can be used to check if an array has been initialized. Always dimension arrays before using LBound/UBound on them.
Best Practices
- Always use: Prefer LBound/UBound over hardcoded array bounds.
- Dimension checking: Verify arrays are dimensioned before use.
- Loop variables: Use the same type as array indices for loop counters.
- Function parameters: Document expected array bounds for functions.
- Error handling: Include checks for array bounds in critical code.
This tutorial covered the FreeBasic LBound
and UBound
keywords with practical examples showing their usage in different scenarios.
Author
List all FreeBasic Tutorials.