Small Basic – Variables as Named Constants

Variables as Named Constants

Try out the program in the Listing; it displays the phone numbers of your friends in a TextWindow. (At least, we think they’re your friends.)


 1 '

 2  'Displays the phone numbers of your friends


 4  TextWindow.Write("Frank")

 5  TextWindow.CursorLeft = 10

 6  TextWindow.WriteLine("(109) 212 7653")


 8  TextWindow.Write("Tony")

 9  TextWindow.CursorLeft = 10

10  TextWindow.WriteLine("(109) 534 2335")


12  TextWindow.Write("Lisa")

13  TextWindow.CursorLeft = 10

14  TextWindow.WriteLine("(109) 753 8765")



Frank     (109) 212 7653

Tony      (109) 534 2335

Lisa      (109) 753 8765


So far so good. The phone numbers line up just like we want! Everything looks nice until you decide to add these statements to include Christopher:


TextWindow.CursorLeft = 10

TextWindow.WriteLine("(109) 753 8765")



Frank     (109) 212 7653

Tony      (109) 534 2335

Lisa       (109) 753 8765

Christophe(109) 753 8765


Because his name has more than 10 letters, the last letter’s cut off! To fix this, you go through your program and change each number 10 (the second column) to 12. All’s good until you add a new friend, Michelle-Marie, to your contact list! You have to go back again and change each number 12.

If you’re adding your 500 closest Facebook friends, then these changes will take way too long!

Instead of typing the same number (like 10) over and over in your program, you can just use a variable (for example, COL2_POS), give it 10 at the start of the program, and then use statements like this in your program:

TextWindow.CursorLeft = COL2_POS


Then if you want to change the start position of the second column from 10 to 12, you just need to change this one line in your source code:

COL2_POS = 12 ' Update it to any position


COL2_POS is a variable called a named constant;  it’s a constant variable that never changes. Using named constants can make a program easier to read and much easier to update. The number 10 might not be clear, but the identifier COL2_POS explains that this number (10) is the position of column 2.

We recommend you define named constants at the top of the program and give them descriptive names that use capital letters only (so all caps mean that we’re defining a constant). If the name has multiple words, we’ll use underscores between them (like MAX_VALUE).


Constants in Small Basic

Although we plan to use the COL2_POS variable as a named constant, Small Basic doesn’t know that. This means that you can still change the value of COL2_POS inside your program if you want.

Other programming languages have special keywords for defining constant things; these languages won’t let you change the value of a variable that was declared as a constant. To be as easy to learn as possible, Small Basic doesn’t have a keyword for constants.



This program converts a temperature from degrees Fahrenheit to Celsius:

degF = 68

degC = (degF - 32) * (5/9)

degC = Math.Round(degC)

TextWindow.WriteLine( degF + " F = " + degC + " C")


Rewrite the program using these named constants:




NOTE: For reasons that date back to the FORTRAN programming language in the 1960s, single letters like i, j, and k, are still common for naming integer variables (variables intended to store whole numbers).


Head to to download it and learn all about it!


Small and Basically yours,

   - Ninja Ed & Majed Marji

Comments (1)
  1. Computers Today (part 1 of 6)…/computers-today.aspx


    CS SPOTLIGHT: Girls in computer programming… why it matters!!!…/cs-spotlight-girls-in-computer-programming-why-it-matters.aspx

    Computational Thinking – Videos & Papers by Jeannette Wing…/computational-thinking-videos-amp-papers-by-jeannette-wing.aspx

Comments are closed.

Skip to main content