This article explains about variable especially in Small Basic.  Following picture is the intellisense icon for variable.

In this article:


What is Variable

Variable is a container or a memo for a value in many program languages.  Variable contains number or string.  Constant number or string cannot be changed.  But variable can be changed.  So important point is that you can generalize your code by using variable.  For example, following program can only calculate 1 + 1.

TextWindow.WriteLine(1+1)

But following program can calculate many kind of addition.  In this case, a and b are variables.

TextWindow.Write("a? ")
a = TextWindow.ReadNumber()
TextWindow.Write("b? ")
b = TextWindow.ReadNumber()
TextWindow.Write("a+b=")
TextWindow.WriteLine(a+b)

Variable Type in Small Basic

Many of other programming languages distinguish types of variables such like Integer, Real or String.  For example, internal expressions are different between integer and string.

Type Value Internal Expression
integer -1 FFFFFFFFh
string (text) "-1" 2Dh,31h

But in Small Basic, type of variables is only one. Internally that is called Primitive.  So following two lines cause same results.

x = -1
x = "-1"

More detail about Small Basic variable type is posted here as blog article by litdev.

Array

Array is a variable which contains one or more dimensional sequence of values.  Most simple way to use array is following 1-D array with continuous index numbers.

f[0] = 0
f[1] = 1
For i = 2 To 10
  f[i] = f[i - 2] + f[i - 1]
EndFor

If you can't use array, the code above will become:

f0 = 0
f1 = 1
f2 = f0 + f1
f3 = f1 + f2
f4 = f2 + f3
f5 = f3 + f4
f6 = f4 + f5
...
f10 = f8 + f9

So using array helps to manipulate many data as sequence of values (numbers or texts).  Internally, array is expressed as text with special characters "=" and ";".  If you see the array f above with TextWIndow.WriteLine(f), you can get following text.

0=0;1=1;2=1;3=2;4=3;5=5;6=8;7=13;8=21;9=34;10=55;

One entry of the array is expressed as "<index>=<value>;".  And entries are concatenated into a text.  If there are some special characters in index or value, another special character "\" appears.  This "\" is called escape character.  Following is the sample code.

a["="] = ";"
TextWindow.WriteLine(a) ' \==\;;

If you make a 2-D or more dimensional array, it contains complicated special characters.

One more important point of Small Basic arrays is that you can use text as index.  In some other languages, this is called associative array or hash.

Naming Convention

Variable name should start with alphabet character or underscore ("_") and be trailed alphabet, digit or underscore. This rule is the same for Subroutine or Label name. And in Small Basic, case of alphabet is not sensitive.

Scope of Variable

In Small Basic, all variables are global. This means that variables can be accessed from any place in a program globally. Scope means that the valid range of variables.  So, the scope of variables in Small Basic is all through the program.   Most modern programming languages have other type of variables such as a local variable.  In Visual Basic, a variable defined in subroutine is local.  The scope of the local variable is in the subroutine only.  There is no way to change the scope of variables in Small Basic. 

Permanence of Variable

Variable is memorized in a memory of your computer. So, variables will break when the program or the computer shut down.  Permanent data should be output to local file with File operations.

Sample Program

Variable Simulator (VMW773) - This program shows the contents of variables.  Substitution statements can be typed in the top left text box.  The bottom left text box shows the history of the substitute statements.  In the right side, simulated variables are displayed.


See Also

Other Languages