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: 
matrixebiz
Roku Guru

Brightscript Sub and Function Routine

Hello, just wondering the difference between a Sub routine and a Function routine.

Are Sub routines always ran in the code automatically and a Function routine needs to be called upon within the code?

Or do you always have to call upon them both within the code script for them to run? Thx
0 Kudos
18 REPLIES 18
squirreltown
Roku Guru

Re: Brightscript Sub and Function Routine

"matrixebiz" wrote:
Or do you always have to call upon them both within the code script for them to run? Thx

Yes. The difference is that a function can return something.
Kinetics Screensavers
0 Kudos
EnTerr
Roku Guru

Re: Brightscript Sub and Function Routine

sub is just a special case of function
Sub is a function that does not return a value (formally, its return type is "as void"). Also, from what i remember you cannot have an anonymous/lambda sub literal:
myFun = function(): ? "fun": end function   ' can haz '
mySub = sub(): ? "sub": end sub ' cannot haz '

Consider "sub" as a shortcut when writing procedures/subroutines.
0 Kudos
Komag
Roku Guru

Re: Brightscript Sub and Function Routine

Maybe I'm missing some usefulness, but I don't have a single sub, only Functions, with some "AS VOID" where needed. Is there a benefit to using sub?
0 Kudos
belltown
Roku Guru

Re: Brightscript Sub and Function Routine

"Komag" wrote:
Maybe I'm missing some usefulness, but I don't have a single sub, only Functions, with some "AS VOID" where needed. Is there a benefit to using sub?

No benefit that I can see. I very rarely use subs, except for main(), which I don't expect to ever return a value.
0 Kudos
EnTerr
Roku Guru

Re: Brightscript Sub and Function Routine

Indeed. Sub is syntax sugar like "don't" - you "do not" have to use the short form  🙂 
0 Kudos
renojim
Community Streaming Expert

Re: Brightscript Sub and Function Routine

It's much quicker to type Sub/End Sub than Function ... as void/End Function😄
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.
marcelo_cabral
Roku Guru

Re: Brightscript Sub and Function Routine

As a retro-developer (there is such word ?) that worked with Visual Basic 1.0 up to VB.Net I also use Sub a lot, when there is no return value I just think there is no point to write a Function "as void"

🙂
0 Kudos
EnTerr
Roku Guru

Re: Brightscript Sub and Function Routine

Guys, you do know that in B/S declaring the return and arguments types is largely in the "good manners" category and does not speed up the code, right?
Just checking  🙂
0 Kudos
NND
Visitor

Re: Brightscript Sub and Function Routine

You can declare sub with return value and use it as a function. One real difference is the one mentioned by EnTerr, no anonymous subs.
0 Kudos
Need Assistance?
Welcome to the Roku Community! Feel free to search our Community for answers or post your question to get help.

Become a Roku Streaming Expert!

Share your expertise, help fellow streamers, and unlock exclusive rewards as part of the Roku Community. Learn more.