Page 1 of 1

[Implemented] Optional Parameters in Procedures

Posted: Sat Jul 10, 2004 4:20 pm
by syntax error
One thing I miss in PureBasic is having optional parameters in Procedure calls. Look at these PB examples:

Code: Select all

Circle(x, y, Radius [, Color]) 
SaveImage(#Image, FileName$ [, ImagePlugin [, Flags]])
OpenWindow(#Window, x, y, InnerWidth, InnerHeight, Flags, Title$ [, ParentWindowID])
Optional parameters could be implemented by allowing them to have default values:

Code: Select all

Procedure Text(t$,x,y,Centered=#FALSE)
  If Centered=#TRUE
    x=((x-TextLength(t$))/2)
  EndIf
  Locate x,y : DrawText(t$)
EndProcedure

; 2 ways to call the above procedure
Text("hello",50,20)
Text("hello",320,20,#TRUE) ; centre text at position 320

Posted: Sat Jul 10, 2004 5:10 pm
by blueznl
default values are (in this form) not a good idea, as you cannot see if there was a parameter or not :-)

unless you add some additional logic to detect that, like a IsParameter function, so you can test if a parameter was actually given or not... Also, you need to be able to specify if a parameter is optional or not.

Code: Select all

Procedure alpha( a.l , b.l , c.l = 5 , d.l , e.l )
  ;
  ; a.l, b.l and d.l must exist, c.l and e.l are optional
  ;
  ;
  If IsParameter(c.l)
    ;
    ; ah, it was passed
    ;
  EndIf
EndProcedure

alpha( 1,2,,3)
in the above, the existence of a default value for a parameter indicates it can be left out during the call

just food for thought :-)

..

Posted: Sat Jul 10, 2004 6:26 pm
by NoahPhense
I'm not in any hurry for this to become standard.. as I want Fred to be able
to take his time with it..

Code: Select all

Procedure.l beta(a.l, b.l, opt_c.l, d.l)
  x = 0
  If opt_c = 00
    x = a+b+d
  Else
    x = a+b+opt_c+d
  EndIf
  ProcedureReturn x
EndProcedure

Procedure.s kapa(a.s, b.s, opt_c.s, d.s)
  If opt_c = "00"
    newSTR.s = a+""+b+"a "+d
  Else
    newSTR.s = a+""+b+""+opt_c+""+d
  EndIf
  ProcedureReturn newSTR
EndProcedure

Debug beta(1, 2, 00, 4)
Debug beta(1, 2, 3, 4)
Debug beta(10, 10, x, 2000)
Debug beta(10, 10, abracadabra, 10)

Debug kapa("Yeah, ", "this is ", "kind of a ", "mess.")
Debug kapa("Yeah, ", "this is ", "00", "mess.")
- np

Posted: Sun Jul 11, 2004 11:11 am
by syntax error
This is how Blitz does it:

Code: Select all

Procedure Test(name$,x,y,color=0)
Here, the color parameter is set to 0 by default unless the programmer decides to pass an alternative value:

Code: Select all

Test "poo",10,20 ; color parameter is 0

Code: Select all

Test "poo",10,20,1234 ; the color parameter is 1234

Posted: Sun Jul 11, 2004 1:41 pm
by fweil
I agree with such a Proc(a, b, c=something) to declare an optional parameter.

Should the "default" value be a constant or could it be a variable would help.

I like this way.

Rgrds

Posted: Sun Jul 11, 2004 1:45 pm
by blueznl
i think it should be a constant, but an addtitional function inside the procedure could check if it was passed, if not that's where to assign it the value of a variable

Posted: Sun Jul 11, 2004 7:17 pm
by Flype
or perhaps, something like this...

Code: Select all

Declare test()
Declare test(a.l)

Procedure test()
  Debug "default"
EndProcedure

Procedure test(a.l)
  Debug a
EndProcedure

Posted: Sun Jul 11, 2004 8:03 pm
by MadMax
Yes it would be nice to have that, not sure about the syntax though. I'll just let Fred decide :)

Posted: Fri Nov 05, 2004 10:10 pm
by Psychophanta
I need it NOW :!: :x

Posted: Fri Nov 05, 2004 11:07 pm
by Dare2
That idea and the varName=defaultVal syntax is nice.

bump!

Posted: Sat Jul 30, 2005 11:45 am
by pantsonhead
Any more news on optional parameters?
It would be nice if it's possible.
Has Fred expressed an opinion yet?

Posted: Sat Jul 30, 2005 1:02 pm
by DoubleDutch
Tailbite does it, just implement it in the same way.

Posted: Sun Jul 31, 2005 5:49 am
by Fou-Lu
I'd love to see that. I've already tryed to create a procedure this way:

Code: Select all

Procedure something(wherever.l,whenever.l,[shakira.l])
Hoping it would work, but it didn't... :oops:

About the syntax, I liked the Blitz one. IsParameter() seems very annoying. But let's Fred decide that... whatever.
Tailbite does it, just implement it in the same way.
What do you mean? Is that already possible?

Posted: Sun Jul 31, 2005 11:24 am
by DoubleDutch
Take a look at Tailbite, i used the optional parameters to the max in the consolex library.

-Anthony

Posted: Sun Jul 31, 2005 1:37 pm
by Rescator
blueznl wrote:default values are (in this form) not a good idea, as you cannot see if there was a parameter or not :-)

Code: Select all

Procedure alpha( a.l , b.l , c.l = 5 , d.l , e.l )
  ;
  ; a.l, b.l and d.l must exist, c.l and e.l are optional
  ;
  ;
  If IsParameter(c.l)
    ;
    ; ah, it was passed
    ;
  EndIf
EndProcedure

alpha( 1,2,,3)
Hmm! that would be very bad coding! optionals are always at the end.
Even still, there is no need to check if one was set or not,
because the default would be used anyway.
Because that is the reason for optionals in the first place,
to provide a default but useable value in case none was specified
or if the default actualy is the wanted one.

As far as syntax/usage goes, do it the "common" way that most uses.

Procedure alpha( a.l=5 , b.l=5 , c.l = 5 , d.l=5 , e.l=5 )

alpha(1,2,3,4)
alpha(1,2,3)
alpha(1,2)
alpha(1)
alpha()

Here all where optional.
and there is no ,,, mess, and it should be DLL/lib compatible.
(DLL's and libs as far as I know only allow optional args at the end not in the middle).

Procedure alpha( a.l , b.l , c.l , d.l=5 , e.l=5 )

alpha(1,2,3,4)
alpha(1,2,3)
alpha(1,2)

Here only the last two are optional!

Procedure alpha( a.l , b.l , c.l , d.l , e.l )

alpha(1,2,3,4)

And for completness sake, here none are optional.

I would not recommend the way blueznl showed,
as that could cause issues with ProcedureDLL and so on
as I'm not sure DLL's and libs makes it possible to do that or supported at all.
At least I can't ever recall ever seeing a API function or DLL function
that does it, nor can I imagine how to do that properly.
The only language I know that permits this is PHP,
,,, may be practical in a scipting language,
but a proper programing language it would just be a headache.
(either for Fred or us)

So I really have to advice the more common way.
Heck, the common way (only allow optionals at the end,
or all, but never in he middle)
should allow making dll's, so's and .library (Amiga) and (what does mac call it?)
with optionals very easy.

And it would be silly if Procedure and ProcedureDLL differed,
they really have to handle optionals identicaly to maintain continuity!