Usina BRAdvPL Guide
Sign in
← All topics
COMMANDPublished

FUNCTION

[STATIC] FUNCTION <cNome>( [<uParam1>, <uParam2>, ...] )

Declares an AdvPL function, with local parameters and mandatory completion through RETURN.

DeclarationFunctionsRoutinesParametersRETURN
01 · OVERVIEW

Overview

FUNCTION is the command used to declare a function in AdvPL. The function receives a name, may receive parameters and must finish execution with RETURN. When declared as STATIC FUNCTION, it is visible only inside its own source file; when published as USER FUNCTION, it becomes a routine callable externally according to Protheus environment conventions.

02 · SYNTAX

Syntax

[STATIC] FUNCTION <cNome>( [<uParam1>, <uParam2>, ...] )

Parameters

STATIC
ModifierOptional

Restricts function visibility to the source file where it was declared.

cName
IdentifierRequired

Name of the declared function. It should be stable when used by menus, entry points or external calls.

uParamN
Any AdvPL typeOptional

Parameters received by the function. Each parameter behaves as a local variable in the function scope.

RETURN
CommandRequired

Ends the function and optionally returns a value to the caller.

03 · PRACTICAL EXAMPLE

Function with parameters and return

#Include "TOTVS.ch"

User Function ExFunction()
    Local nResultado := SomaValores(10, 5)

    MsgInfo(CValToChar(nResultado), "Resultado")
Return

Static Function SomaValores(nValorA, nValorB)
Return nValorA + nValorB
Expected result

The User Function is the entry point. The Static Function receives local parameters, calculates the result and returns the value to the caller.

04 · PRACTICAL EXAMPLE

Logical return for validation

#Include "TOTVS.ch"

User Function ExValida()
    Local cCodigo := "A001"

    If CodigoValido(cCodigo)
        MsgInfo("Código aceito", "Validação")
    EndIf
Return

Static Function CodigoValido(cCodigo)
Return !Empty(AllTrim(cCodigo))
Expected result

The helper function returns .T. or .F., making the validation contract explicit.

BEST PRACTICES
  • Cada parâmetro declarado entre parênteses é criado como variável local da função.
  • A execução da função deve ser finalizada com RETURN, com ou sem valor de retorno.
  • Use STATIC FUNCTION para rotinas auxiliares internas do fonte.
  • Use USER FUNCTION quando a rotina precisar ser chamada externamente pelo ambiente, menu, ponto de entrada ou outra integração.
  • O TDN registra que uma função pode ser chamada diretamente pelo nome ou pelo operador de alias, conforme o contexto.
COMMON PITFALLS
  • Do not confuse FUNCTION with a function call: FUNCTION declares the routine; using the name executes it.
  • Omitted parameters or parameters in the wrong order change the behavior of the called routine.
  • Functions without a clear return contract are harder to test and maintain.
  • Exposing as USER FUNCTION something that should only be a helper increases source coupling.

Related content

REFERENCES
  1. TOTVS. FUNCTION. TDN Framework. Disponível em: https://tdn.totvs.com/display/public/framework/FUNCTION. Conteúdo consultado em 25/08/2026 e adaptado para guia de referência.
Status
Published
Page created on
Last reviewed on
Original language
Portuguese
Reviewed by
Usina.BR
0 approved comment(s)

Comments

There are no approved comments yet.

Sign in with Google or Microsoft to comment.

Powered by Usina Docs · Alpha