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

“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.

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.

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?

“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.

Indeed. Sub is syntax sugar like “don’t” - you “do not” have to use the short form :slightly_smiling_face:

It’s much quicker to type Sub/End Sub than Function … as void/End Function. :grinning_face_with_smiling_eyes:

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”

:slightly_smiling_face:

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 :slightly_smiling_face:

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.

So with ‘Sub’ I can declare a value to Something without having to return that value back out of the routine like I would have to do with a ‘Function’? or are you meaning something different?

Something = Door
Somthing = Sub() or Somthing = Function()

Sub()
Change Something to = “Window”
End Sub

Function()
Change Something to = “Window”
return window
End Function

“matrixebiz” wrote:
So with ‘Sub’ I can declare a value to Something without having to return that value back out of the routine like I would have to do with a ‘Function’? or are you meaning something different?
You are not required to return something from a function.

Okay, thank you

sometimes I want to conditionally cancel a function with an empty RETURN, and if I plan to do that, I just have to add the AS VOID to the FUNCTION at the top or else I’ll get a Return error.
Ex:

FUNCTION mapScrollOne(dir) AS VOID
 map    = m.drAA.map
 IF NOT m.mAA.flL[map.mapFl][ 1] THEN RETURN
 IF map.mInvert THEN dir = oppFace(dir)
 moveY  = 0
 moveX  = 0
 mSYp   = map.mapScrollY
 mSXp   = map.mapScrollX
 d      = m.sy.sett.scrollSz
 IF       dir = 0: moveY = - d: mSYp = mSYp - d
   ELSEIF dir = 1: moveX =   d: mSXp = mSXp + d
   ELSEIF dir = 2: moveY =   d: mSYp = mSYp + d
   ELSEIF dir = 3: moveX = - d: mSXp = mSXp - d
 END IF
 IF checkMapRoom(dir, mSYp, mSXp)
   map.mapScrollY = mSYp
   map.mapScrollX = mSXp
   offsetMap(moveY, moveX)
   playSound("menuForw", 0, 0, 0, FALSE, 1, 1)
 END IF
END FUNCTION

“Komag” wrote:
sometimes I want to conditionally cancel a function with an empty RETURN, and if I plan to do that, I just have to add the AS VOID to the FUNCTION at the top or else I’ll get a Return error.
Or you could return 0

“Komag” wrote:
sometimes I want to conditionally cancel a function with an empty RETURN, and if I plan to do that, I just have to add the AS VOID to the FUNCTION at the top or else I’ll get a Return error.
You can also use Return from a Sub the same way.

“squirreltown” wrote:

“Komag” wrote:
sometimes I want to conditionally cancel a function with an empty RETURN, and if I plan to do that, I just have to add the AS VOID to the FUNCTION at the top or else I’ll get a Return error.
Or you could return 0
Functions are Integers by default? If I have to declare AS INTEGER then it’s easier to type AS VOID and save myself the trouble later on of typing that 0. But if you can do 0 by default without the AS VOID that’s nice to know.

“Komag” wrote:
Functions are Integers by default?
Functions and variables in B/S are “as Dynamic”, unless otherwise explicitly declared. That means you can return ANY of The Types.

Ah yes, if I would have applied my noggin a little harder I might have realized/remembered that! At least, I knew it regarding variables.