This article illustrates about subroutine in Microsoft Small Basic programming language.


What Is A Subroutine

A subroutine is a package of statements.  Subroutines help to reduce the same code in programs.  Following icon represents a subroutine as Intellisense in Small Basic IDE (editor).

Where to Write Subroutines

You can write subroutines anywhere in your program.  Statements not included in subroutines are treated as in main routine.  So you can write program as follows.

A() ' statement calls subroutine A
Sub A
EndSub
B() ' statement calls subroutine B
Sub B
EndSub

But to distinguish main and sub, main statements are better to be written in one place as following list.

A() ' statement calls subroutine A
B() ' statement calls subroutine B
Sub A
EndSub
Sub B
EndSub

Subroutine Name

As well as variable name, subroutine name starts with any alphabet or underscore "_" and trails with them or also digits.  Not as syntax but as manner, subroutine name usually starts with upper case character and variable name usually starts with lower case character.

Parameters

In some other programming languages such as Visual Basic, one or some parameters can be passed to a subroutine.  But in Small Basic, there is no syntax to pass parameters to subroutines.  So many people tried to pass values with many ways to subroutines in Small Basic.

Most popular way to pass parameters to a subroutine in Small Basic is to use variables.  In following sample, variable n is used as a parameter and variable f is used to contain return value of the subroutine Factorial.

n = 5
Factorial()
TextWindow.WriteLine(n + "!=" + f)
Sub Factorial
  ' param n
  ' return f = n!
  f = 1
  For i = 2 To n
    f = f * i
  EndFor
EndSub

Recursive Call

Recursive call is that a subroutine calls itself.

Sample

Hilbert Curve (CMN910) - This program calls subroutines recursively.


See Also

Other Languages