PxPlus User Forum

Twitter Twitter Twitter

Author Topic: Accepting variable number of arguments in a function call  (Read 1618 times)

Frank Dreßler

  • Guest
Accepting variable number of arguments in a function call
« on: December 12, 2019, 08:35:20 AM »
I was trying to accept a variable number of arguments to a function so that all of them appear as elements of an array in the function.

Code: [Select]
x = new("VARARG_TEST")

x'test(123)
! Expected result inside test(): params[1] = 123

x'test(11, 22, 33, 44, 55)
! Expected result inside test(): params[1] = 11, params[2] = 22, params[3] = 33, params[4] = 44, params[5] = 55

drop object x

My current approach can be seen in the following code. "enter_vararg" enters the arguments into an array "params{all}"

Code: [Select]
def class "VARARG_TEST"
function test(*) test
end def

test:
gosub enter_vararg ! Make arguments available inside params{all}
print params{all}
return

enter_vararg:
dim params[*]

skip$ = ""
while 1
execute "enter " + skip$ + " x", err=*break
params[*] = x
skip$ += "*,"
wend
return

This looks a bit clunky because of skip$ and execute. Is there a better way to solve this problem?

Frank Dreßler

  • Guest
Re: Accepting variable number of arguments in a function call
« Reply #1 on: December 12, 2019, 09:00:35 AM »
"enter_vararg" is like doing the following:
Code: [Select]
enter_vararg:
dim params[*]
local x
enter x, err=*return; params[*] = x
enter *, x, err=*return; params[*] = x
enter *, *, x, err=*return; params[*] = x
enter *, *, *, x, err=*return; params[*] = x
enter *, *, *, *, x, err=*return; params[*] = x
! ...
return

koenv

  • Member
  • **
  • Posts: 12
    • View Profile
Re: Accepting variable number of arguments in a function call
« Reply #2 on: December 12, 2019, 09:32:33 AM »
TCB(20) tells you how many arguments there are, but it doesn't check for non-numbers.
STK(PROPERTIES) tells you how many arguments there are and the type of each argument (https://manual.pvxplus.com/PXPLUS/functions/stk.htm).

Using that you can create something like this:
Code: [Select]
00010 !
00020 DEF CLASS "vararg_test"
00030 FUNCTION test(*)TEST
00040 END DEF
00050 !
00060 !
00070 TEST:
00080 LET properties$=STK(PROPERTIES)
00090 IF properties$="" THEN EXIT 23
00100 IF POS("Nn"^properties$)>0 THEN EXIT 26
00110 !
00120 LET properties_count=LEN(properties$)
00130 LET iolist$="IOLIST "
00140 FOR i=1 TO properties_count
00150 LET iolist$+="param"+STR(i)+","
00160 NEXT i
00170 LET iolist$=CPL(STP(iolist$,"R",","))
00180 !
00190 ENTER IOL=iolist$
00200 DIM params[1:properties_count]
00210 READ DATA FROM REC(iolist$) TO params{ALL}
00220 !
00230 FOR i INDEX params{ALL}
00240 PRINT i,params[i]
00250 NEXT i
00260 RETURN 1
00270 !

->x=new("vararg_test")
->x'test(123)
 1 123
->x'test(11,22,33,44,55)
 1 11
 2 22
 3 33
 4 44
 5 55

James Zukowski

  • Diamond Member
  • *****
  • Posts: 297
    • View Profile
Re: Accepting variable number of arguments in a function call
« Reply #3 on: January 09, 2020, 01:19:23 PM »
Has the STK(Properties) function been updated to reflect read-only arrays?
James Zukowski
Sr. Developer - J&E

BRAND>SAFWAY
Brand Industrial Services

Mike King

  • Diamond Member
  • *****
  • Posts: 3811
  • Mike King
    • View Profile
    • BBSysco Consulting
Re: Accepting variable number of arguments in a function call
« Reply #4 on: January 09, 2020, 01:47:52 PM »
No -- we actually hit a bit of syntax issue with regards to this as a string array returns "A" and a numeric array returns "a".

Generally when we had a Read only element we returned the lower case (e.e. a Numeric element is "N" but if read only then its "n").

This posed a problem when it came to arrays and we didn't want to break anything.
Mike King
President - BBSysco Consulting
eMail: mike.king@bbsysco.com

James Zukowski

  • Diamond Member
  • *****
  • Posts: 297
    • View Profile
Re: Accepting variable number of arguments in a function call
« Reply #5 on: January 09, 2020, 01:52:37 PM »
I noticed that, hence the question.

How about "X/x", or "P/p" for String/numeric 'Protected' arrays?
James Zukowski
Sr. Developer - J&E

BRAND>SAFWAY
Brand Industrial Services