Page 1 of 1

Optional Arguments

Posted: Tue Apr 28, 2015 11:27 pm
by Phantomas
Example of Procedure with some Optional Arguments:

Code: Select all

Procedure.i proc(one.i, two.i = 1337, three.i = 1337)
  Debug one
  Debug two
  Debug three
EndProcedure
What if I want call this procedure with declare only first (one) and third (three, optional) arguments? I do not want declare second (two, optional) parameter and want keep it default (1337).

In PAWN I can do that:

Code: Select all

proc(0, _, 1338)
"_" means "skip it" and this argument will be equal 1337.

In PureBasic I can not do that:

Code: Select all

proc(0, 1337, 1338)
I should enter second parameter and can not skip it.

Re: Optional Arguments

Posted: Wed Apr 29, 2015 12:48 am
by Tenaja
No, but if you hardcode the default value in the definition, you can hardcode the value in the call. (Best with a pre-defined #Constant.)

Re: Optional Arguments

Posted: Wed Apr 29, 2015 5:46 am
by Little John
When using named parameters for a procedure, the order of the parameters doesn't matter, and thus handling of optional parameters is very flexible. It can be implemented e.g. this way (at the expense of speed).

Re: Optional Arguments

Posted: Wed Apr 29, 2015 6:48 am
by Josh
Phantomas wrote:"_" means "skip it" and this argument will be equal 1337.
Think is a useful feature and not hard to implement in PB

+1

Re: Optional Arguments

Posted: Wed Apr 29, 2015 9:26 am
by c4s
+1 for any kind of implementation.

But how about just keeping the desired optional argument empty (instead of using "_") as requested and discussed e.g. here: Procedures - optional parameter more flexible?! That would probably be even easier and more reasonable to implement because "_" is a valid variable name.

Re: Optional Arguments

Posted: Fri May 01, 2015 11:22 am
by Trond
If the optional parameter can't have the value -1, it can be coded like this:

Code: Select all

#_ = -1

Procedure.i proc(one.i, two.i = 1337, three.i = 1337)
  If two = #_
    two = 1337
  EndIf
  Debug one
  Debug two
  Debug three
EndProcedure

proc(0, #_, 1338)

Re: Optional Arguments

Posted: Thu Jul 09, 2015 3:02 pm
by Fred
Yes, in PB we often use this as well trough the #PB_Ignore or #PB_Default constant.