procedure parametercount()

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
jassing
Addict
Addict
Posts: 1885
Joined: Wed Feb 17, 2010 12:00 am

procedure parametercount()

Post 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.
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: procedure parametercount()

Post 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
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
jacdelad
Addict
Addict
Posts: 1993
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: procedure parametercount()

Post by jacdelad »

...or simply give the optional parameters a default optional value that is never being passed (if that is possible!).
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
BarryG
Addict
Addict
Posts: 4135
Joined: Thu Apr 18, 2019 8:17 am

Re: procedure parametercount()

Post 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.
AZJIO
Addict
Addict
Posts: 2143
Joined: Sun May 14, 2017 1:48 am

Re: procedure parametercount()

Post 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)
User_Russian
Addict
Addict
Posts: 1520
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: procedure parametercount()

Post 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()
Last edited by User_Russian on Sat Apr 29, 2023 12:45 pm, edited 2 times in total.
Jeromyal
Enthusiast
Enthusiast
Posts: 216
Joined: Wed Jul 17, 2013 8:49 am

Re: procedure parametercount()

Post 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)
Jeromyal
Enthusiast
Enthusiast
Posts: 216
Joined: Wed Jul 17, 2013 8:49 am

Re: procedure parametercount()

Post 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
juergenkulow
Enthusiast
Enthusiast
Posts: 581
Joined: Wed Sep 25, 2019 10:18 am

Re: procedure parametercount()

Post 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  
----
Please ask your questions, because switch on the cognition apparatus decides on the only known life in the universe.Wersten :DDüsseldorf NRW Germany Europe Earth Solar System Flake Bubble Orionarm
Milky Way Local_Group Virgo Supercluster Laniakea Universe
User_Russian
Addict
Addict
Posts: 1520
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: procedure parametercount()

Post by User_Russian »

I tested the code only on Windows.
Post Reply