How to create an optional parameter in ADVPL?

4

In several functions documented by Totvs there are optional parameters. I would like to create an optional parameter in my function, how to do it?

I want to make the updEnvio function get a vector (of recno s) as an optional parameter.

User Function updEnvio(cTabela)
    Local cSQL := ""
    Local cCampo := IIF(SUBS(cTabela,1,1)=='S', SUBS(cTabela,2,2), cTabela)
    Local nStatus := 0

    cSQL := ""
    cSQL += " UPDATE " + RetSqlName(cTabela)
    cSQL += " SET " + cCampo + "_YGSENV = 'S' "
    cSQL += " WHERE 1=1 "
    cSQL += " AND " + cCampo + "_FILIAL = '" + xFilial(cTabela) + "' "
    cSQL += " AND " + cCampo + "_YGSENV <> 'S' "

    nStatus := TcSqlExec(cSQL)

    If (nStatus < 0)
        conout("Oops, TCSQLError: " + TCSQLError())
    Endif
Return

In a rough way, I wanted to transform it so that if the vector was empty, it would update the whole table; however, if it had content, only the elements whose recno s are described in that vector. Using C ++ logic to be, I would write the default value of the parameter in its declaration. So my ADVPL pseudocode looks like this:

User Function updEnvio(cTabela, aRecnos := {})
    Local cSQL := ""
    Local cCampo := IIF(SUBS(cTabela,1,1)=='S', SUBS(cTabela,2,2), cTabela)
    Local nStatus := 0
    Local i

    cSQL := ""
    cSQL += " UPDATE " + RetSqlName(cTabela)
    cSQL += " SET " + cCampo + "_YGSENV = 'S' "
    cSQL += " WHERE 1=1 "
    cSQL += " AND " + cCampo + "_FILIAL = '" + xFilial(cTabela) + "' "
    cSQL += " AND " + cCampo + "_YGSENV <> 'S' "

    If LEN(aRecnos) > 0
      cSql += " AND R_E_C_N_O_ IN ("
      cSql += STR(aRecnos[1])
      For i := 2 to len(aRecnos)
        cSql += "," + STR(aRecnos[i])
      Next i
      cSql += ")"
    Endif

    nStatus := TcSqlExec(cSQL)

    If (nStatus < 0)
        conout("Oops, TCSQLError: " + TCSQLError())
    Endif
Return
    
asked by anonymous 14.11.2018 / 15:40

2 answers

3

First let's take a correct nomenclature: What's the difference between parameter and argument? . Parameters are never optional, or they exist or do not exist, it's not like in JavaScript. Arguments can be optional. And what you want to know is whether the parameters can have their values initialized if the argument relating to it is not the past.

It is not possible with this syntax, the language does not support what you want in the question. It has a trick to facilitate what you want (by the way I created it in 1994, and even today it's a include resource) using default .

User Function updEnvio(cTabela, aRecnos)
    Local cSQL := ""
    Local cCampo := IIF(SUBS(cTabela,1,1)=='S', SUBS(cTabela,2,2), cTabela)
    Local nStatus := 0
    Local i
    Default aRecnos := {}

    cSQL := ""
    cSQL += " UPDATE " + RetSqlName(cTabela)
    cSQL += " SET " + cCampo + "_YGSENV = 'S' "
    cSQL += " WHERE 1=1 "
    cSQL += " AND " + cCampo + "_FILIAL = '" + xFilial(cTabela) + "' "
    cSQL += " AND " + cCampo + "_YGSENV <> 'S' "

    If LEN(aRecnos) > 0
      cSql += " AND R_E_C_N_O_ IN ("
      cSql += STR(aRecnos[1])
      For i := 2 to len(aRecnos)
        cSql += "," + STR(aRecnos[i])
      Next i
      cSql += ")"
    Endif

    nStatus := TcSqlExec(cSQL)

    If (nStatus < 0)
        conout("Oops, TCSQLError: " + TCSQLError())
    Endif
Return

I placed GitHub for future reference .

With default the value will only be assigned if nothing comes as a parameter. That is, they took what I created and turned it into official syntax, when in fact it had an obvious better syntax when they resolved to put in the language.

As a curiosity open the file protheus.ch that you have access to do customization and look for the excerpt (not to mention that is identical to what I did, I used Iif() always not to confuse visually with a command If with parentheses):

#xcommand DEFAULT <uVar1> := <uVal1> ;
      [, <uVarN> := <uValN> ] => ;
    <uVar1> := If( <uVar1> == nil, <uVal1>, <uVar1> ) ;;
   [ <uVarN> := If( <uVarN> == nil, <uValN>, <uVarN> ); ]

A curiosity that few know is that you can use the preprocessor, which is much more powerful than C, and create named arguments, which are even more useful because in essence the language does not work with objects (there is a very poor and inefficient implementation, so avoid use.

    
14.11.2018 / 15:57
-1

In ADVPL the parameters of a function are optional. Parameters not supplied come with nil value.

Example:

#include "protheus.ch"

user function test(xParm)
  if xParm != nil
    conout("xParm=" + xParm)
  else
    conout("nao recebeu parametros!")
  end if

  return

That's it.

    
18.11.2018 / 22:41