Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
ov2015
Visitor

Wrong number of function parameters

Hi

I have a function

Function showScreen(sc1 As Object, sc2 As Object)
...
if sc2 <> invalid then
' do something here
end if
...
End Function


I need to call this function from various places. When I have a call as below, I don't have a problem
showScreen(sc1, sc2)


I would like to know if we can define the second parameter as an optional parameters, so I can use the function with only one parameter like
showScreen(sc1)


Currently, I am getting error "Wrong number of function parameters" while using only 1 parameter
0 Kudos
7 REPLIES 7
ov2015
Visitor

Re: Wrong number of function parameters

I was acting dumb, it was similar to how PHP does. Hope someone will benefit from my post.

Solution below:


Function showScreen(sc1 As Object, sc2 = invalid As Object)
...
if sc2 <> invalid then
' do something here
end if
...
End Function
0 Kudos
belltown
Roku Guru

Re: Wrong number of function parameters

It doesn't help that the BrightScript Language Reference does not document the syntax for optional function parameters. It mentions that optional parameters may be declared, but the syntax definition for the FUNCTION statement doesn't depict an optional parameter declaration with a default value. It does show an example with a list of function statements, two of which have optional parameters, but with no explanation that that's what they are. It's one of those things you're just supposed to know, I guess.
0 Kudos
ov2015
Visitor

Re: Wrong number of function parameters

"belltown" wrote:
It doesn't help that the BrightScript Language Reference does not document the syntax for optional function parameters. It mentions that optional parameters may be declared, but the syntax definition for the FUNCTION statement doesn't depict an optional parameter declaration with a default value. It does show an example with a list of function statements, two of which have optional parameters, but with no explanation that that's what they are. It's one of those things you're just supposed to know, I guess.


😄 Yes I do agree with ROKU not having proper documentation. I have faced few of those problems myself.
I had a PHP background so just tried if it works, otherwise will have been searching in those documents for hours and waiting for some one to reply.
0 Kudos
Komag
Roku Guru

Re: Wrong number of function parameters

This is totally new to me, interesting 🙂
0 Kudos
RokuMarkn
Visitor

Re: Wrong number of function parameters

I've expanded the description of default parameter values in the doc.

--Mark
0 Kudos
belltown
Roku Guru

Re: Wrong number of function parameters

"RokuMarkn" wrote:
I've expanded the description of default parameter values in the doc.

--Mark

Awesome!
0 Kudos
EnTerr
Roku Guru

Re: Wrong number of function parameters

"RokuMarkn" wrote:
I've expanded the description of default parameter values in the doc.

Thanks Mark!
As an addendum, there are some caveats to the way default params work, as demonstrated by this:
BrightScript Debugger> f = function(x=upTime(0)): return x: end function: ? f()
1060.72

BrightScript Debugger> ? f()
1063.5

BrightScript Debugger> f = function(x=main): return x: end function: ? f()
<Function: main>

BrightScript Debugger> y = 7: f = function(x=1/y): return x: end function: ? f(8): ? f()
8
Use of uninitialized variable. (runtime error &he9)

In other words: if needed, the default value expression is evaluated at call time (i.e. every time a call is made - not at all obvious since say in Python defaults are calculated at definition-execution time; has some... schmonsequences). And the expression does not "see" the locals but sees the globals (user or system).