IonicWind Software

IWBasic => General Questions => Topic started by: billhsln on July 21, 2019, 09:12:00 PM

Title: Variable Arguments
Post by: billhsln on July 21, 2019, 09:12:00 PM
I have a need to do variable arguments in a SUB.  I have looked at the example and I am surprised that it actually requires you to tell it how many parameters you are sending.  Isn't there some one in our bunch who has a better way?  I can deal with what we have, but just thought that there would be a more a more elegant way to do this.

Thanks,
Bill
Title: Re: Variable Arguments
Post by: LarryMc on July 21, 2019, 10:18:34 PM
surely when you call the sub you know how many parameters you are going to be sending it each specific time which may be varying each time?
Title: Re: Variable Arguments
Post by: fasecero on July 22, 2019, 06:18:14 AM
There's no elegant way to do this. On the contrary, a 'dirty' way to avoid telling how many parameters you are sending is by using an "unwanted value" as the last entry. Using -1 at this one

$INCLUDE "windowssdk.inc"
$INCLUDE "stdarg.inc"

DECLARE CDECL cmpFunc(a:POINTER,b:POINTER),INT

OPENCONSOLE
PRINT "First sorting"
SortNumbers(0, 8, 2, 3, -1)
PRINT

PRINT "Second sorting"
SortNumbers(0, 12, 4, 5, 533, 1, 43, 544, 34, -1)
PRINT

PRINT "Press any key to exit"
DO:UNTIL INKEY$ <> ""
CLOSECONSOLE

SUB SortNumbers(INT refVar, ...)
POINTER pParam = VA_START(refVar)
INT numbers[100]
INT nCount = 0

FOR j = 1 TO 100
numbers[j-1] = *<INT>pParam ' access the variable using dereferencing operator

IF numbers[j-1] <> - 1 THEN
nCount += 1 ' how many numbers do we have?
pParam += 4 ' use pointer math to retrieve successive arguments
ELSE
BREAKFOR
ENDIF
NEXT j

DEF v = &numbers + 0:POINTER
qsort(v, nCount, 4, &cmpFunc)

FOR j = 1 TO nCount
PRINT numbers[j-1]
NEXT j
ENDSUB

SUB cmpFunc(a:POINTER,b:POINTER),INT
RETURN *<INT>a - *<INT>b
ENDSUB
Title: Re: Variable Arguments
Post by: billhsln on July 22, 2019, 09:14:06 AM
Larry, yes, while it is true that I would know how many parameters I will be passing, I was just thinking that it would be better to do some thing like what is done when passing parameters to a program.

Fasecero, That sounds like what I do for when I use DATA statements.  That will work.

Bill