[Implemented] Optional Parameters in Procedures

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
syntax error
User
User
Posts: 93
Joined: Tue Jan 13, 2004 5:11 am
Location: Midlands , UK

[Implemented] Optional Parameters in Procedures

Post 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
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post 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 :-)
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

..

Post 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
syntax error
User
User
Posts: 93
Joined: Tue Jan 13, 2004 5:11 am
Location: Midlands , UK

Post 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
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Post 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
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post 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
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post 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
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
MadMax
Enthusiast
Enthusiast
Posts: 237
Joined: Mon Oct 06, 2003 11:56 am

Post by MadMax »

Yes it would be nice to have that, not sure about the syntax though. I'll just let Fred decide :)
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post by Psychophanta »

I need it NOW :!: :x
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

That idea and the varName=defaultVal syntax is nice.
@}--`--,-- A rose by any other name ..
pantsonhead
User
User
Posts: 39
Joined: Fri Mar 26, 2004 1:47 pm
Location: London, UK
Contact:

bump!

Post by pantsonhead »

Any more news on optional parameters?
It would be nice if it's possible.
Has Fred expressed an opinion yet?
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Post by DoubleDutch »

Tailbite does it, just implement it in the same way.
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
User avatar
Fou-Lu
Enthusiast
Enthusiast
Posts: 201
Joined: Tue Jul 12, 2005 8:30 am
Location: I'm pretty sure this is all a nightmare
Contact:

Post 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?

~Fou-Lu (aka Lørd Cinneris (actually Elias Sant'Ana))

Image Image
User avatar
DoubleDutch
Addict
Addict
Posts: 3220
Joined: Thu Aug 07, 2003 7:01 pm
Location: United Kingdom
Contact:

Post by DoubleDutch »

Take a look at Tailbite, i used the optional parameters to the max in the consolex library.

-Anthony
https://deluxepixel.com <- My Business website
https://reportcomplete.com <- School end of term reports system
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Post 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!
Post Reply