Page 1 of 1

procedure parametercount()

Posted: Fri Apr 28, 2023 9:07 pm
by jassing
yes, I know we can have defaults

Code: Select all

procedure ABC( a,b,c.s="")
but what if the user passes "" (or whatever 'default' we pick)
it'd be nice to have a function to tell how many parameters were, indeed, passed.

Re: procedure parametercount()

Posted: Fri Apr 28, 2023 9:48 pm
by STARGĂ…TE
When a procedure is called, always all parameters are passed, the default arguments as well.
Therefore such a function would give always the same number inside a procedure (in your case 3).

Code: Select all

Procedure Test(A.i, B.i=2, C.i=3)

EndProcedure

Test(1)

Code: Select all

; Procedure Test(A.i, B.i=2, C.i=3)
_Procedure0:
  MOV    qword [rsp+8],rcx
  MOV    qword [rsp+16],rdx
  MOV    qword [rsp+24],r8

Code: Select all

; Test(1)
  MOV    r8,qword 3
  MOV    rdx,qword 2
  MOV    rcx,qword 1
  CALL  _Procedure0

Re: procedure parametercount()

Posted: Fri Apr 28, 2023 10:00 pm
by jacdelad
...or simply give the optional parameters a default optional value that is never being passed (if that is possible!).

Re: procedure parametercount()

Posted: Sat Apr 29, 2023 2:21 am
by BarryG
The idea of default procedure parameters is that they get used when the user specifically omits them. So, you need to rethink your approach, such as not allowing parameters at all if there's a chance the user won't specify them. Remember: they're default parameters, not optional parameters.

Re: procedure parametercount()

Posted: Sat Apr 29, 2023 5:49 am
by AZJIO
The author means the "Default" keyword, which does not oblige you to know the default value. For example, is the line counting from 0 or from 1? Inserted the keyword "Default" and the countdown starts from the beginning. There is such a keyword in AutoIt3.

Code: Select all

Procedure Test(a, b=-1, c=1, d=0)
	If b = Default
		b=-1
	EndIf
	If c = Default
		c=1
	EndIf
	If d = Default
		d=0
	EndIf
...
Debug Test(45, Default, 15)

Re: procedure parametercount()

Posted: Sat Apr 29, 2023 12:42 pm
by User_Russian
viewtopic.php?p=553643#p553643

Code: Select all

Structure ArrI
  i.i[0]
EndStructure

CompilerIf  #PB_Compiler_Processor = #PB_Processor_x86
  ProcedureC Test(Count)
CompilerElseIf #PB_Compiler_Processor = #PB_Processor_x64
  ProcedureC Test(Count, x, y, z)
CompilerElse
  CompilerError "Not supported"
CompilerEndIf
  Protected *Param.ArrI=@Count+SizeOf(Count)
  Protected i
  
  PrintN("Count = "+Count)
  Print("Parameters: ")
  For i=0 To Count-1
    Print(Str(*Param\i[i])+"  ")
  Next
  
  PrintN(#CRLF$+"----")
EndProcedure

OpenConsole()
CallCFunctionFast(@Test(), 2, 1, 2)
CallCFunctionFast(@Test(), 10, 2, 8, 20, 100, 4, 1234, 10000, 10, 16, 80)
Input()

Re: procedure parametercount()

Posted: Sat Apr 29, 2023 12:43 pm
by Jeromyal
Something like this?

Code: Select all

#Default = -9999

Procedure.s Test(a, b=-1, c=1, d=0)
  dv.s = "Defaults: "
	If b = #Default
	  b=-1
	  dv + " b "
	EndIf
	If c = #Default
	  c=1
	  dv + " c "
	EndIf
	If d = #Default
	  d=0
	  dv + " d "
	EndIf
	If Len(dv) = 10 : dv ="" : EndIf
	
	ProcedureReturn dv
EndProcedure

Debug Test(45, #Default, 15)
Debug Test(45, 10, 1)
Debug Test(45, 4, 15, 0)
Debug Test(45, #Default, #Default, #Default)

Re: procedure parametercount()

Posted: Sat Apr 29, 2023 12:50 pm
by Jeromyal
User_Russian wrote: Sat Apr 29, 2023 12:42 pm viewtopic.php?p=553643#p553643

Code: Select all

Structure ArrI
  i.i[0]
EndStructure

CompilerIf  #PB_Compiler_Processor = #PB_Processor_x86
  ProcedureC Test(Count)
CompilerElseIf #PB_Compiler_Processor = #PB_Processor_x64
  ProcedureC Test(Count, x, y, z)
CompilerElse
  CompilerError "Not supported"
CompilerEndIf
  Protected *Param.ArrI=@Count+SizeOf(Count)
  Protected i
  
  PrintN("Count = "+Count)
  Print("Parameters: ")
  For i=0 To Count-1
    Print(Str(*Param\i[i])+"  ")
  Next
  
  PrintN(#CRLF$+"----")
EndProcedure

OpenConsole()
CallCFunctionFast(@Test(), 2, 1, 2)
CallCFunctionFast(@Test(), 10, 2, 8, 20, 100, 4, 1234, 10000, 10, 16, 80)
Input()
Wow!, I like this. This I am saving as a snipit for the future. Thank you for sharing this. :D

Re: procedure parametercount()

Posted: Sun Apr 30, 2023 7:56 pm
by juergenkulow
@User_Russian

Code: Select all

CallCFunctionFast(@Test(), 2, 1, 2)
CallCFunctionFast(@Test(), 10, 2, 8, 20, 100, 4, 1234, 10000, 10, 16, 80)
Linux x64 6.01 C Opt:
Count = 2
Parameters: 2  1  
----
Count = 10
Parameters: 139691493705216  1  140723430533024  -179862365584584960  140723430533480  0  4211280  4367686  1234  10000  
----
Linux x64 C
Count = 2
Parameters: 0  140724837002400  
----
Count = 10
Parameters: 0  140724837002304  0  14170240  0  0  0  0  14170240  0  
----



Linux x64 ASM 
Count = 2
Parameters: 1  2  
----
Count = 10
Parameters: 2  8  20  140734790934816  4  139982607847488  1  4372198  1234  10000  
----

Re: procedure parametercount()

Posted: Sun Apr 30, 2023 8:01 pm
by User_Russian
I tested the code only on Windows.