Roku Developer Program

Developers and content creators—a complete solution for growing an audience directly.
cancel
Showing results for 
Search instead for 
Did you mean: 
matrixebiz
Level 7

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
squirreltown
Level 8

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
Level 8

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
Level 9

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
Level 7

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.
https://github.com/belltown/
0 Kudos
EnTerr
Level 8

Re: Brightscript Sub and Function Routine

Indeed. Sub is syntax sugar like "don't" - you "do not" have to use the short form  Smiley Happy 
0 Kudos
renojim
Level 8

Re: Brightscript Sub and Function Routine

It's much quicker to type Sub/End Sub than Function ... as void/End FunctionSmiley Very Happy
0 Kudos

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"

Smiley Happy
0 Kudos
EnTerr
Level 8

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  Smiley Happy
0 Kudos
NND
Level 7

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