FreeBasic Dim Keyword
last modified June 16, 2025
The FreeBasic Dim
keyword is used to declare variables and arrays.
It allocates memory for data storage and optionally initializes variables.
Understanding Dim
is fundamental to FreeBasic programming.
Basic Definition
In FreeBasic, Dim
stands for "dimension" and is used to declare
variables with specific data types. It can declare single variables or arrays
of variables. The syntax allows for initialization during declaration.
Variables declared with Dim
have local scope by default when used
inside procedures. At module level, they have module-wide scope. Proper use
of Dim
ensures efficient memory management.
Simple Variable Declaration
This example shows basic variable declaration using the Dim
keyword.
Dim age As Integer Dim fname As String Dim price As Double = 19.99 age = 25 fname = "John Doe" Print "Name: "; fname Print "Age: "; age Print "Price: "; price
Here we declare three variables of different types. The age
and
fname
variables are declared first then assigned values later.
price
is initialized during declaration. Each variable has a
specific data type.
Multiple Variable Declaration
Dim
can declare multiple variables of the same type in one statement.
Dim x As Integer, y As Integer, z As Integer Dim firstName As String, lastName As String = "Smith" x = 10 y = 20 z = x + y firstName = "John" Print "Full name: "; firstName; " "; lastName Print "Sum: "; z
This example declares three integer variables in one line and two string variables in another. Note that initialization can be done for some variables while leaving others uninitialized. This makes code more concise.
Array Declaration
Dim
is used to declare arrays with specified dimensions.
Dim numbers(1 To 5) As Integer Dim names(0 To 2) As String = {"Alice", "Bob", "Charlie"} For i As Integer = 1 To 5 numbers(i) = i * 10 Next For i As Integer = 0 To 2 Print names(i) Next For i As Integer = 1 To 5 Print numbers(i) Next
Here we declare two arrays: an integer array with indices 1 to 5 and a string array with indices 0 to 2. The string array is initialized during declaration. Arrays are fundamental for storing collections of data.
Dynamic Arrays
FreeBasic allows dynamic arrays whose size can be changed at runtime.
Dim dynamicArray() As Integer Dim size As Integer = 3 ReDim dynamicArray(1 To size) For i As Integer = 1 To size dynamicArray(i) = i * 100 Next Print "Original array:" For i As Integer = 1 To size Print dynamicArray(i) Next size = 5 ReDim Preserve dynamicArray(1 To size) Print "Resized array:" For i As Integer = 1 To size Print dynamicArray(i) Next
This demonstrates dynamic array usage. We first declare an empty array, then
use ReDim
to set its size. ReDim Preserve
resizes
while keeping existing values. Dynamic arrays offer flexibility in memory usage.
Multi-dimensional Arrays
Dim
can declare arrays with multiple dimensions.
Dim matrix(1 To 3, 1 To 3) As Integer Dim row As Integer, col As Integer ' Initialize matrix For row = 1 To 3 For col = 1 To 3 matrix(row, col) = row * col Next Next ' Print matrix For row = 1 To 3 For col = 1 To 3 Print matrix(row, col); " "; Next Print Next
This creates a 3x3 matrix (two-dimensional array). We use nested loops to initialize and print the array. Multi-dimensional arrays are useful for representing grids, tables, or matrices in mathematical operations.
Type Declaration
Dim
can declare variables of user-defined types.
Type Person name As String age As Integer height As Double End Type Dim student As Person Dim teacher As Person = {"Mr. Smith", 45, 1.82} student.name = "Alice" student.age = 20 student.height = 1.65 Print "Student: "; student.name; ", "; student.age; ", "; student.height Print "Teacher: "; teacher.name; ", "; teacher.age; ", "; teacher.height
Here we define a Person
type and declare two variables of this
type. One is initialized during declaration, the other is assigned values
field by field. User-defined types help organize related data into logical units.
Static Variables
Dim
can declare static variables that retain their values between calls.
Sub Counter() Static Dim count As Integer count += 1 Print "Count: "; count End Sub For i As Integer = 1 To 5 Counter() Next
This example shows a static variable inside a procedure. Unlike regular local
variables, static variables maintain their values between procedure calls.
The count
variable increments with each call to Counter
.
Best Practices
- Explicit Types: Always specify variable types for clarity.
- Descriptive Names: Use meaningful variable names.
- Initialization: Initialize variables when possible.
- Scope: Declare variables in the smallest needed scope.
- Arrays: Prefer 0-based or 1-based indexing consistently.
This tutorial covered the FreeBasic Dim
keyword with practical
examples showing its usage in different scenarios. Mastering Dim
is essential for effective FreeBasic programming.
Author
List all FreeBasic Tutorials.