Page 1 of 1
Function with Unknown number of parameters - Possible?
Posted: Sun Jun 07, 2015 12:14 pm
by Rathinagiri
Is it possible in Harbour to have a function defined with unknown number of parameters?
For example, I want to define like this:
Function MyFunction( Parameter1, Parameter2, Parameter3,..,ParameterN)
nParameters := pcount()
?nParamters
return nil
Re: Function with Unknown number of parameters - Possible?
Posted: Sun Jun 07, 2015 1:31 pm
by Carlos Britos
Hi
maybe this is what you want:
Code: Select all
PROCEDURE TEST_1_3v( a, b, c, ... )
LOCAL i := "i"
?? ProcName( 0 ), " received: ", PCount()
? "i=", i
? "a=", a
? "b=", b
? "c=", c
FOR i := 1 TO PCount()
? i, "=", hb_PValue( i )
NEXT
FOR EACH i IN hb_AParams()
? i:__enumindex, "-", i
NEXT
Inkey( 0 )
RETURN
samples in \harbour\tests\varparam.prg
Function with Unknown number of parameters - Possible?
Posted: Sun Jun 07, 2015 2:14 pm
by Pablo César
Going a little bit deeper...
Trying with this:
Code: Select all
Function Main(...)
Local aArg := hb_aParams()
Local aPar := ScanParam(aArg)
If aPar[1]="F"
SetDisplayFullScreen(1)
..
If aPar[2]="P"
cPrinter:="LPT1"
..
Function ScanParam(aArg)
Local aRet:={"",""}, nLen:=HMG_Len(aArg), i
For i=1 To nLen
If hb_USubStr(aArg[i],1,1)="/"
If hb_USubStr(HMG_Upper(aArg[i]),2,1)="F"
aRet[1]:="F"
ElseIf hb_USubStr(HMG_Upper(aArg[i]),2,1)="W"
aRet[1]:="W"
Else
aRet[1]:="N"
Endif
Else
aRet[2]:=aArg[i]
Endif
Next
Return aRet
This example of parameters, let you process all parameters and also identify it. Because sometimes users do not follows the strict sequency.
The big point is "..." after parenthesis at Main function to receive all parameters.
I hope be useful.

Re: Function with Unknown number of parameters - Possible?
Posted: Sun Jun 07, 2015 4:00 pm
by esgici
Rathinagiri wrote:Is it possible in Harbour to have a function defined with unknown number of parameters?
Please look at
here SEH2.zip\SEHandlr.Prg\PROCEDURE KKOUT() ( Line : 113 )
Happy HMG'ing

Re: Function with Unknown number of parameters - Possible?
Posted: Sun Jun 07, 2015 4:24 pm
by Rathinagiri
Wow! That is so great about this language.
Thank you friends for your immediate and timely help.
Re: Function with Unknown number of parameters - Possible?
Posted: Tue Jun 09, 2015 2:29 am
by dhaine_adp
Simply nice, Pablo, Carlos and Esgici.

Re: Function with Unknown number of parameters - Possible?
Posted: Tue Jun 09, 2015 10:30 am
by quartz565
Thank you all !!