Roku Developer Program

Developers and content creators—a complete solution for growing an audience directly.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Komag
Level 10

Is it possible to have set up optional Function parameters?

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.
0 Kudos
4 REPLIES 4
squirreltown
Level 9

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

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 Screensavers
0 Kudos
renojim
Community Streaming Expert

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

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
Roku Community Streaming Expert

Help others find this answer and click "Accept as Solution."
If you appreciate my answer, maybe give me a Kudo.

I am not a Roku employee.
RokuNB
Roku Employee
Roku Employee

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

"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.
0 Kudos
Komag
Level 10

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

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! 😄
0 Kudos