Forum Discussion

ov2015's avatar
ov2015
Visitor
9 years ago

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

7 Replies

  • "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).
  • 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
  • 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.
  • "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.
  • This is totally new to me, interesting 🙂
  • I've expanded the description of default parameter values in the doc.

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

    --Mark

    Awesome!