March 28, 2024, 04:09:32 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Variable Arguments

Started by billhsln, July 21, 2019, 09:12:00 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

billhsln

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
When all else fails, get a bigger hammer.

LarryMc

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?
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

fasecero

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

billhsln

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
When all else fails, get a bigger hammer.