FreeBasic Static Keyword
Last modified October 18, 2026
The FreeBasic Static keyword is used to declare variables that
retain their values between function calls. Unlike local variables, static
variables persist throughout program execution.
Basic Definition
In FreeBasic, Static modifies variable declarations to give them
static storage duration. Static variables are initialized only once and maintain
their values between function calls.
Static variables can be declared at procedure level (local static) or module level (global static). They are useful for maintaining state across multiple function invocations.
Static Local Variable
This example demonstrates a static local variable that persists between calls.
Sub Counter()
Static count As Integer = 0
count += 1
Print "Count: "; count
End Sub
Counter()
Counter()
Counter()
The count variable is declared static inside the Counter
subroutine. It retains its value between calls, incrementing each time. Without
static, it would reset to 0 on each call.
Static vs Non-Static Comparison
This example contrasts static and non-static local variables.
Sub TestVars()
Static staticVar As Integer = 10
Dim nonStaticVar As Integer = 10
staticVar += 5
nonStaticVar += 5
Print "Static: "; staticVar; " Non-static: "; nonStaticVar
End Sub
TestVars()
TestVars()
TestVars()
The static variable maintains its incremented value between calls, while the regular local variable reinitializes each time. This shows the persistence difference between static and automatic variables.
Static Array
Static can be used with arrays to preserve their contents between calls.
Sub ProcessData()
Static arr(3) As Integer
Static initd As Integer
If initd = 0 Then
arr(0) = 1 : arr(1) = 2 : arr(2) = 3 : arr(3) = 4
initd = 1
End If
For i As Integer = 0 To 3
arr(i) *= 2
Print arr(i); " ";
Next
Print
End Sub
ProcessData()
ProcessData()
ProcessData()
The static array is filled with {1,2,3,4} only on the first call (tracked by the
static flag initd); on each call its values are doubled, showing
that the contents persist. Without static, the array would be recreated and
the doubling would start from the same values on each invocation.
Static in Recursive Functions
Static variables are useful in recursive functions to track state.
Function Factorial(n As Integer) As Integer
Static depth As Integer = 0
depth += 1
Print "Depth: "; depth; " n: "; n
If n <= 1 Then
Return 1
Else
Return n * Factorial(n - 1)
End If
End Function
Print "5! = "; Factorial(5)
The static depth variable tracks recursion depth across all
recursive calls. It demonstrates how static variables maintain state
throughout the entire recursion process.
Static Global Variable
Static can limit global variable scope to the current module.
Static Shared sharedValue As Integer = 100
Sub ModifyValue()
sharedValue += 50
Print "Modified value: "; sharedValue
End Sub
ModifyValue()
ModifyValue()
Print "Final value: "; sharedValue
The sharedValue is a module-level static variable declared with
Static Shared, which makes it visible inside procedures. Its
storage is allocated once at program startup and it keeps its value between
calls to ModifyValue.
Static in Classes
Static members in classes maintain single instances across all objects.
Type Counter
Static total As Integer
Dim instanceCount As Integer
Declare Sub Increment()
End Type
Dim As Integer Counter.total = 0
Sub Counter.Increment()
total += 1
instanceCount += 1
Print "Total: "; total; " Instance: "; instanceCount
End Sub
Dim c1 As Counter
Dim c2 As Counter
c1.Increment()
c2.Increment()
c1.Increment()
The static member total is created once, independently of any object,
and is shared among all Counter instances; it must be defined outside the Type
with a Dim statement. instanceCount is unique to each object. This demonstrates class-
level vs instance-level variables.
Static Initialization
Static variables are initialized only once, at program startup.
Function GetID() As Integer
Static id As Integer = 100
id += 1
Return id
End Function
Print "ID 1: "; GetID()
Print "ID 2: "; GetID()
Print "ID 3: "; GetID()
The id variable initializes to 100 only once. Subsequent calls
increment from the last value. This pattern is useful for generating unique
IDs or sequence numbers.
Best Practices
- Use sparingly: Static variables can make code harder to understand.
- Thread safety: Be cautious with statics in multi-threaded code.
- Initialization: Static variables initialize only once.
- Naming: Prefix static variables to indicate their nature.
- Alternatives: Consider class members for object persistence.
This tutorial covered the FreeBasic Static keyword with practical
examples showing its usage in different scenarios.
Author
List all FreeBasic Tutorials.