parâmetrosNames between the vertical bars. They are local to the block and receive the supplied arguments.
AdvPL Guide{ |[parâmetros]| [expressão1, expressão2, ...] }Create storable anonymous functions, receive parameters and evaluate expressions on demand with Eval().
A code block is an AdvPL value of type B (Block), similar to an anonymous function. It may receive parameters and execute one or more comma-separated expressions. Eval() executes the block and returns the result of its last expression. Unlike a regular function, its body accepts expressions and function calls, but not language commands.
{ |[parâmetros]| [expressão1, expressão2, ...] }parâmetrosNames between the vertical bars. They are local to the block and receive the supplied arguments.
expressõesOne or more comma-separated AdvPL expressions, executed from left to right.
retornoThe result of the last expression; an empty block returns NIL.
User Function ExBlockSum()
Local bAdd := { |nValue1, nValue2| Abs(nValue1) + Abs(nValue2) }
Local nResult := Eval(bAdd, 10, -20)
ConOut("Result: " + CValToChar(nResult))
ReturnEval() supplies two arguments and receives 30, the result of the last expression.
User Function ExBlockList()
Local bCalculate := { |nValue| ConOut("Received: " + CValToChar(nValue)), nValue * 2 }
Local nResult := Eval(bCalculate, 5)
MsgInfo("Double: " + CValToChar(nResult))
ReturnExpressions run from left to right and the return value is 10, the result of the last expression.
User Function ExOrdenacao()
Local aNomes := {"CARLA", "ANDREA", "BEATRIZ"}
Local bDecrescente := { |cNome1, cNome2| cNome1 > cNome2 }
ASort(aNomes, , , bDecrescente)
ReturnThe block compares pairs selected by ASort() and enables descending order.
User Function ExReferencia()
Local bIncrementar := { |nValor| nValor += 1 }
Local nContador := 0
Eval(bIncrementar, nContador) // nContador continua 0
Eval(bIncrementar, @nContador) // nContador passa a 1
ReturnWithout @, the block receives a copy; with @, its change reaches the original variable.
User Function ExContexto()
Local nContador := 0
Local bSomar := { |nValor| nContador += nValor }
Eval(bSomar, 5)
ConOut("Contador: " + CValToChar(nContador))
ReturnThe block references and changes nCounter in the Local context where it was created.
© 2026 Usina.BR. All rights reserved. TOTVS, Protheus, Microsiga and their respective logos are trademarks of their owners. AdvPL Guide is independent and uses limited, attributed excerpts justified by educational purposes. See the Terms of Use. Terms of Use
Comments
There are no approved comments yet.
Sign in with Google or Microsoft to comment.