GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Download
  • News
  • Community
  • Resources
  • Language Reference
  • SmallBASIC Manual

This view

  • Original page
  • New address

GeezerVison is showing smallbasic.github.io — SmallBASIC | SUB

Edit History

SUB

SUB foo ([ [BYREF] var1, [BYREF] var2, …, [BYREF] varN])

Home > Reference > Language

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)

x = 9
routine1(x)           ' Output: 9

Sub routine1(x)
  Print x
End

Example 2: Passing x by reference (BYREF x)

x = 9
Print x              ' Output: 9
routine2(x)          ' Output: 9
Print x              ' Output: 10

Sub routine2(Byref x)
  Print x
  x = 10
End

Example 3: Passing x by reference (@x)

x = 9
Print x              ' Output: 9
routine3(x)          ' Output: 9
Print x              ' Output: 10

Sub routine3(@x)
  Print x
  x = 10
End

Example 4: Use the LOCAL keyword for local variables

x = 9
routine4(x)         ' Output: 9 
Print x             ' Output: 9

Sub routine4(y)
  Local x
  Print y
  x = 12            ' x is local, the global variable x will not be changed
End Sub

Example 5: Nested sub procedures

x = 9
routine5(x)         ' Output: 100 (Passing 'x' by value to nested procedures)
Print x             ' Output: 9

Sub routine5(x)
  local x
  x = 100
  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   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.bas   betrayal: crows ii.bas   bezier_pen.bas   biorythms.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  
This page was last edited on Tue, 22 Aug 2023 11:31:03 +0200 | Markdown processed with pandoc 3.1.12.1
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.