ov2015
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-29-2016
07:08 PM
Wrong number of function parameters
Hi
I have a function
I need to call this function from various places. When I have a call as below, I don't have a problem
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
Currently, I am getting error "Wrong number of function parameters" while using only 1 parameter
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 7
ov2015
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-29-2016
07:11 PM
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:
Solution below:
Function showScreen(sc1 As Object, sc2 = invalid As Object)
...
if sc2 <> invalid then
' do something here
end if
...
End Function
belltown
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-29-2016
07:32 PM
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.
ov2015
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-29-2016
08:09 PM
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.

Komag
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-29-2016
10:14 PM
Re: Wrong number of function parameters
This is totally new to me, interesting 🙂

RokuMarkn
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2016
09:34 AM
Re: Wrong number of function parameters
I've expanded the description of default parameter values in the doc.
--Mark
--Mark
belltown
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2016
09:45 AM
Re: Wrong number of function parameters
"RokuMarkn" wrote:
I've expanded the description of default parameter values in the doc.
--Mark
Awesome!
EnTerr
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2016
01:18 PM
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).