Your Digital Media Has Never Looked So Good

 
User avatar
Komag
Topic Author
Posts: 661
Joined: Fri Aug 22, 2014 3:42 am

Is it possible to have set up optional Function parameters?

Sun Aug 06, 2017 6:54 am

I can't think of how this can be possible, but it would be convenient for me. Currently, calling a Function must have the exact correct number of parameters. Is there a way to set up so a Function can have optional parameters? I can only think of encasing all the parameters in a single array, therefore there is ONE parameter, and just varying what I include in the array.
 
User avatar
squirreltown
Posts: 698
Joined: Sun Apr 21, 2013 2:20 pm

Re: Is it possible to have set up optional Function parameters?

Sun Aug 06, 2017 7:28 am

Yep, that's a good way. I do this all the time, mostly to run the same function repeatedly with different parameters.

params = { x:1, y:2, title:"This!"}

myFunction (varr, m, params)

local = params.x
Kinetics Screensaver (kineticsscreensaver), Kinetics³ Screensaver(kinetics3), Kinetics Painter Screensaver (kineticspainter), Kinetics Splash Screensaver (kineticssplash)
 
renojim
** Valued Community Member **
Posts: 3108
Joined: Mon Feb 15, 2010 1:35 pm

Re: Is it possible to have set up optional Function parameters?

Sun Aug 06, 2017 12:17 pm

I assume you really mean "optional", not just a parameter with a default value, although I can't really see the difference.
Sub mySub(parm1 as String, parm2=invalid)
   if parm2 <> invalid then
      'do something
   end if
   'do more stuff
End Sub

mySub("hello")
mySub("hello","world")

-JT
 
User avatar
RokuNB
Posts: 217
Joined: Fri Mar 31, 2017 2:22 pm

Re: Is it possible to have set up optional Function parameters?

Sun Aug 06, 2017 4:27 pm

renojim wrote:
I assume you really mean "optional", not just a parameter with a default value, although I can't really see the difference.

Right, it's "the same difference" :). One can declare default values for some at the end of the list of arguments - and can check for default value as a flag if it was omitted. In other lagnguages (e.g. Lua) this is so idiomatic that does not have to spec it - if a function is called will less than the declared arguments, the rest are padded with `nil` (AKA null, invalid). This can be argued double-edged sword.

Coming from other scripting languages - like say Python - you would have heard the above approach as "default argument values". Another approach of Keyword Arguments can easily be simulated in BrS by passing a dictionary with key-value pairs.
 
User avatar
Komag
Topic Author
Posts: 661
Joined: Fri Aug 22, 2014 3:42 am

Re: Is it possible to have set up optional Function parameters?

Thu Aug 10, 2017 7:44 pm

Wow, I just got to test that out, that's exactly what I was talking about, and it's something I didn't know about due to still be a newbie! Thanks! :D

Who is online

Users browsing this forum: No registered users and 3 guests