SUB
SUB foo ([ [BYREF] var1, [BYREF] var2, …, [BYREF] varN])
Declare a sub procedure foo
. The optional parameters
var1
to varN
can be all data types supported
by SmallBASIC. Sub procedures do not return a value but can return
argument values when declared as BYREF. A sub procedure ends with the
keywords END or END SUB. The keyword LOCAL can be used to restrict the
scope of a variable to the sub procedure only. Sub procedures can be
nested within a sub procedure.
With SUB (and FUNC) you can divide your program into many logical, independent and reusable blocks, instead of writing a long “spaghetti code”. Instead of using GOSUB and GOTO you should prefere to use sub procedures or functions.
For more information, see FUNC, LOCAL and BYREF.
Example 1: Passing x by value (default)
9
x = ' Output: 9
routine1(x)
Sub routine1(x)
Print x
End
Example 2: Passing x by reference (BYREF x)
9
x = Print x ' Output: 9
' Output: 9
routine2(x) Print x ' Output: 10
Sub routine2(Byref x)
Print x
10
x = End
Example 3: Passing x by reference (@x)
9
x = Print x ' Output: 9
' Output: 9
routine3(x) Print x ' Output: 10
Sub routine3(@x)
Print x
10
x = End
Example 4: Use the LOCAL keyword for local variables
9
x = ' Output: 9
routine4(x) Print x ' Output: 9
Sub routine4(y)
Local x
Print y
12 ' x is local, the global variable x will not be changed
x = End Sub
Example 5: Nested sub procedures
9
x = ' Output: 100 (Passing 'x' by value to nested procedures)
routine5(x) Print x ' Output: 9
Sub routine5(x)
local x
100
x =
routine6(x)Sub routine6(x)
Print x
End Sub
End Sub
Code samples using SUB
000 hello.bas
003 conditional branching.bas
004 loops.bas
005 challenge.bas
100lines.bas
2048.bas
2048.bas
3d block lettes.bas
3d wire cube v1.bas
3d wire cube.bas
3d_palmx.bas
3d_torus.bas
3dtictac.bas
3dtorus.bas
3dttt.bas
3dttt.bas
7gables.bas
agendus.bas
analog clock.bas
anball 1.0.bas
anomail.bas
Another center finder.bas
another look at trig functions.bas
autumn scene.bas
bairstow.bas
balls in 3d cube.bas
balls in 3d cube.bas
balls.bas
betrayal: crows ii.bas
Language
AND
AS
BAND
BG
BOR
BYREF
CALL
CASE
CATCH
CONST
DECLARE
DEF
DO
ELIF
ELSE
ELSEIF
END
END TRY
ENDIF
EQV
EXIT
FALSE
FI
FOR
FUNC
GOSUB
GOTO
IF
IFF
IMP
IN
LABEL
LET
LIKE
LOCAL
LSHIFT
MDL
MOD
NAND
NEXT
NOR
NOT
ON
OR
REM
REPEAT
RETURN
RSHIFT
SELECT
STEP
STOP
SUB
THEN
THROW
TO
TRUE
TRY
UNTIL
USE
USG
USING
WEND
WHILE
XNOR
XOR
If there is insufficient information on this page and you wish learn more about SUB,
please send an email to smallbasic@gmail.com. You can help to improve information about
SUB by submitting a pull request,
click View Source for details. Note, an offline language reference text file is also available - see the Download section.