GLOBAL name AS typeGLOBAL name() AS type [subscripts]
The GLOBAL statement declares variables that are visible and accessible from all procedures throughout the entire program. Global variables exist for the lifetime of the program and provide a mechanism for sharing data between different functions and subroutines without passing parameters.
Global variables must be declared in the main source file, outside of any FUNCTION or SUB block. They are typically placed near the top of the file, after any #COMPILE or #INCLUDE directives but before any procedure definitions.
While global variables are convenient, they should be used sparingly. Excessive use of globals can lead to code that is difficult to debug and maintain due to unexpected side effects.
| Parameter | Description |
|---|---|
name | The identifier name of the variable. Must be unique at the global scope and must not conflict with any procedure name. |
AS type | The data type. Supported types include LONG, DWORD, INTEGER, BYTE, WORD, SINGLE, DOUBLE, QUAD, CUR, EXT, STRING, and user-defined TYPEs. |
subscripts | Optional. Specifies array bounds for global arrays. Same syntax as LOCAL arrays. |
#COMPILE EXE
GLOBAL g_AppName AS STRING
GLOBAL g_MaxRetries AS LONG
GLOBAL g_Config(1 TO 10) AS STRING
SUB InitializeApp()
g_AppName = "PBXA64 Application"
g_MaxRetries = 3
g_Config(1) = "OptionA"
g_Config(2) = "OptionB"
END SUB
FUNCTION GetAppInfo() AS STRING
FUNCTION = g_AppName + " (Retries: " + STR$(g_MaxRetries) + ")"
END FUNCTION
FUNCTION PBMAIN() AS LONG
InitializeApp()
PRINT GetAppInfo()
FUNCTION = 0
END FUNCTION
; GLOBAL variables are placed in the .data section of the PE binary:
section .data
g_AppName db 256 dup(0) ; string buffer
g_MaxRetries dq 0 ; initialized to 0
g_Config dq 10 dup(0) ; array of 10 string descriptors
; Read: x = g_MaxRetries mov rax, [g_MaxRetries] ; direct memory access ; Write: g_MaxRetries = 3 mov rax, 3 mov [g_MaxRetries], rax ; store to global memory ; String assignment: g_AppName = "PBXA64" lea rdi, [g_AppName] lea rsi, [__pb_str_const_1] mov rcx, 256 call [__imp_memcpy] ; copy string to global buffer ; Array element: g_Config(1) = "OptionA" ; Calculates offset for index 1 and stores string descriptor
PBMAIN is called.LOCAL or STATIC variable with the same name as a global, the local variable shadows (hides) the global within that procedure.EXPORT modifier.THREAD GLOBAL or thread-local global storage in the current version.