Function with Unknown number of parameters - Possible?

Issues and Discussions related to Harbour

Moderator: Rathinagiri

Post Reply
User avatar
Rathinagiri
Posts: 5471
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Contact:

Function with Unknown number of parameters - Possible?

Post 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
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.
Carlos Britos
Posts: 245
Joined: Sat Aug 02, 2008 5:03 pm

Re: Function with Unknown number of parameters - Possible?

Post 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
Regards/Saludos, Carlos (bcd12a)
User avatar
Pablo César
Posts: 4059
Joined: Wed Sep 08, 2010 1:18 pm
Location: Curitiba - Brasil

Function with Unknown number of parameters - Possible?

Post 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. :D
HMGing a better world
"Matter tells space how to curve, space tells matter how to move."
Albert Einstein
User avatar
esgici
Posts: 4543
Joined: Wed Jul 30, 2008 9:17 pm
DBs Used: DBF
Location: iskenderun / Turkiye
Contact:

Re: Function with Unknown number of parameters - Possible?

Post 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 :D
Viva INTERNATIONAL HMG :D
User avatar
Rathinagiri
Posts: 5471
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Contact:

Re: Function with Unknown number of parameters - Possible?

Post by Rathinagiri »

Wow! That is so great about this language. :)

Thank you friends for your immediate and timely help.
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.
User avatar
dhaine_adp
Posts: 457
Joined: Wed Aug 06, 2008 12:22 pm
Location: Manila, Philippines

Re: Function with Unknown number of parameters - Possible?

Post by dhaine_adp »

Simply nice, Pablo, Carlos and Esgici. :)
Regards,

Danny
Manila, Philippines
User avatar
quartz565
Posts: 667
Joined: Mon Oct 01, 2012 12:37 pm
Location: Thessaloniki, Greece
Contact:

Re: Function with Unknown number of parameters - Possible?

Post by quartz565 »

Thank you all !!
Best Regards,
Nikos.

os: Windows Server 2019 - 64
Post Reply