ZetCode

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.

static_local.bas
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.

static_comparison.bas
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.

static_array.bas
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.

static_recursive.bas
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_global.bas
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.

static_class.bas
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.

static_init.bas
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

This tutorial covered the FreeBasic Static keyword with practical examples showing its usage in different scenarios.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all FreeBasic Tutorials.