ZetCode

FreeBasic Shared Keyword

Last modified October 18, 2026

The FreeBasic Shared keyword allows variables to be accessed across different scopes within a module. It bridges the gap between global and local variable scoping.

Basic Definition

In FreeBasic, Shared declares variables that are visible within a module but not globally. They persist between function calls while avoiding global namespace pollution.

Shared variables are declared at module level but can be accessed by all procedures within that module. They provide controlled access compared to global variables.

Simple Shared Variable

This example demonstrates basic usage of a shared variable.

shared_basic.bas
Dim Shared counter As Integer = 0

Sub IncrementCounter()
    counter += 1
    Print "Counter: "; counter
End Sub

IncrementCounter()
IncrementCounter()
IncrementCounter()

Here we declare a shared counter variable. The IncrementCounter subroutine can access and modify it. The variable maintains its value between calls, because it is a module-level variable that exists for the whole program run.

Shared vs Global Variables

This example compares shared and global variable behavior.

shared_vs_global.bas
Dim Shared moduleVar As String
Dim moduleVarPrivate As String

moduleVar = "Module"
moduleVarPrivate = "Private"

Sub ShowVars()
    Print "Shared var: "; moduleVar
End Sub

ShowVars()
Print "Non-shared var (module code): "; moduleVarPrivate

Only moduleVar is accessible inside the subroutine. The variable moduleVarPrivate is declared at module level without Shared, so it is visible only to the module-level code; referring to it inside ShowVars causes a "Variable not declared" compile error. Also note that a string variable cannot be initialized in its declaration when it is shared, so the values are assigned at run time instead.

Shared in Multiple Procedures

Shared variables can be used across multiple procedures in a module.

shared_multiple.bas
Dim Shared userStatus As String

Sub SetStatus(newStatus As String)
    userStatus = newStatus
End Sub

Sub PrintStatus()
    Print "Current status: "; userStatus
End Sub

SetStatus("Active")
PrintStatus()
SetStatus("Inactive")
PrintStatus()

The userStatus variable is shared between two procedures. SetStatus modifies it, while PrintStatus reads it. This demonstrates shared state management within a module.

Shared Arrays

Arrays can also be declared as shared for module-wide access.

shared_array.bas
Dim Shared values(1 To 5) As Integer

Sub FillArray()
    For i As Integer = 1 To 5
        values(i) = i * 10
    Next
End Sub

Sub PrintArray()
    For i As Integer = 1 To 5
        Print values(i); " ";
    Next
    Print
End Sub

FillArray()
PrintArray()

The shared array values is accessible to both procedures. FillArray populates it, and PrintArray displays the contents. Shared arrays are useful for module-level data storage.

Shared with Static Variables

This example combines shared and static variables.

shared_static.bas
Dim Shared total As Integer = 0

Function AddToTotal(value As Integer) As Integer
    Static calls As Integer = 0
    calls += 1
    total += value
    Print "Call "; calls; ": Added "; value; ", Total: "; total
    Return total
End Function

AddToTotal(10)
AddToTotal(20)
AddToTotal(30)

The shared total accumulates values across function calls, while the static calls tracks invocation count locally. This shows how shared and static variables can work together.

Shared in Nested Scopes

Shared variables are visible to every procedure in the module, so one procedure can call another and both can use the same variable.

shared_nested.bas
Dim Shared outerVar As String

Sub InnerSub()
    Print "Inner: "; outerVar
    outerVar = "Modified"
End Sub

Sub OuterSub()
    outerVar = "Outer"
    Print "Outer: "; outerVar
    InnerSub()
    Print "Outer after inner: "; outerVar
End Sub

OuterSub()

The shared outerVar is accessible in both OuterSub and InnerSub. FreeBASIC does not allow a Sub to be declared inside another procedure, so InnerSub is a separate module-level procedure. Changes made in InnerSub persist in OuterSub, demonstrating that shared variables are visible across procedures in the module.

Shared with User-Defined Types

User-defined types can be declared as shared for module-wide use.

shared_udt.bas
Type Person
    name As String
    age As Integer
End Type

Dim Shared user As Person

Sub SetPerson(n As String, a As Integer)
    user.name = n
    user.age = a
End Sub

Sub PrintPerson()
    Print "Name: "; user.name; ", Age: "; user.age
End Sub

SetPerson("Alice", 30)
PrintPerson()

The shared Person variable user is accessible to both procedures. One sets the values, while the other displays them. This pattern is common for module-level configuration or state.

Best Practices

This tutorial covered the FreeBasic Shared 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.