ZetCode

FreeBasic Static Keyword

last modified June 16, 2025

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 data(3) As Integer = {1, 2, 3, 4}
    
    For i As Integer = 0 To 3
        data(i) *= 2
        Print data(i); " ";
    Next
    Print
End Sub

ProcessData()
ProcessData()
ProcessData()

The static array doubles its values on each call, showing the values persist. Without static, the array would reset to {1,2,3,4} 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 sharedValue As Integer = 100

Sub ModifyValue()
    sharedValue += 50
    Print "Modified value: "; sharedValue
End Sub

ModifyValue()
ModifyValue()
Print "Final value: "; sharedValue

The sharedValue is static at module level, making it invisible to other modules. It behaves like a global variable but with restricted scope.

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

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 total is shared among all Counter instances, while 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.