Variable
scope
Scope determines where a variable can be accessed and how long its value remains available during execution.
Local nNumero := 10The identifier states that nNumero belongs to the function local scope.
01LOCAL
Current function
LOCAL
Current function
Created on each function activation. Recursive calls receive independent sets of local variables.
- Visibility
- Only the declaring routine
- Lifetime
- Until the function returns
User Function Pai()
Local nVar := 10
Filha()
Return .T.
Static Function Filha()
// nVar is not visible here
Return02STATIC
Routine or file
STATIC
Routine or file
Inside a function, it remains restricted to that routine and retains its value between calls. Outside routines, it can be accessed in the current source file.
- Visibility
- Depends on declaration location
- Lifetime
- While the environment is running
User Function Contador()
Static nChamadas := 0
nChamadas++
ConOut(nChamadas)
Return03PRIVATE
Creating function and descendant calls
PRIVATE
Creating function and descendant calls
It may be declared or created implicitly by assignment. A Private with the same name in a called function temporarily hides the previous one.
- Visibility
- Dynamic scope
- Lifetime
- Until the creating function returns
User Function Pai()
Private nVar := 10
Filha()
Return .T.
Static Function Filha()
ConOut(nVar) // 10
Return04PUBLIC
Entire environment
PUBLIC
Entire environment
Remains available after the creating function returns. Without explicit initialization, its value is logical false (.F.).
- Visibility
- Global, unless hidden by a Private
- Lifetime
- Until execution ends
User Function CriarStatus()
Public lSistemaAtivo := .T.
Return
User Function ConsultarStatus()
ConOut(lSistemaAtivo)
ReturnDeclare your intent
Prefer `LOCAL` for temporary data and `STATIC` when state truly needs to be retained. Use `PRIVATE` and `PUBLIC` only when dynamic or global visibility is part of the routine design.
- SILVA, Waterloo Ferreira da. A Linguagem AdvPL. 2004, p. 22-24. Content summarized and reviewed; examples adapted.
- TOTVS. Variable Context within a Program. TDN.
- TOTVS. Creating and Assigning Variables. TDN.
- TOTVS. Microsiga Protheus SDK. TDN — additional reference.
