Optional parameters for procedures?

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

Is this possible? Or an idea for a future release?

Example: Take the following procedure... is there a way to call it
with or without the second parameter, so that if the second one is
ommitted, it just assumes a default value?

Code: Select all

Procedure Test(one,two)
  If two=0 ; Perhaps not specified?
    two=2
  EndIf
EndProcedure
;
Test(1) ; Currently gives an error because only 1 parameter was passed.

PB - Registered PureBasic Coder
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fweil.

...,

Don't know how it will be easy or not to change this, but I am also interested in such a feature.

By the way in some cases I had to move heaven and earth in some cases to code features with optional parameters.


Francois Weil
14, rue Douer
F64100 Bayonne
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by TheBeck.

Another way to do this is to check how many parameters were passed to the procedure and then set the parameter "two" if only one parameter is detected. I imagine this is not currently possible.

eg:

Code: Select all

Procedure Test(one,two)
  If NumberOfParameters < 2
    two=2
  EndIf
EndProcedure
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by SoulTaker.

Hi Guys,

This would need to be added to the PureBasic Procedure Function, like VB's Optional Key Word.

In VB its:

Function Test(Byval A1, Optional A2 As String)
End Function

So Fred would need to add an Optional Key Word to the Procedure Function and check it.



Abit BD7-II Raid P4 1.9 gHz 384 Ram Xtasy GFroce 3 TI 200 CD-RW DirectX 9.0 Beta 3 Sound Blaster Live! XP Pro, Registered PureBasic User Version 3.40 For Windows.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Franco.
So Fred would need to add an Optional Key Word to the Procedure Function and check it.
The Optional Key Word should work with 'And' and 'Or' too.
Example:

Procedure Test1(Parameter1.l, Optional Parameter2.s, And Parameter3.l, Or Optional Parameter2.l)

Procedure Test2(Parameter1.l, Optional Parameter2.s, Optional Parameter3.l,)


Have a nice day...

Franco
Post Reply