Variables


What is a Variable?

A variable is a name given to a storage place in the computers memory. You have similar storage places in your home. For example, you have different jars and containers in your home for storing different items in your house (such as a jar for cookies, a jar for peanut butter, a bowl for sugar, a carton for milk, etc.). Each of these containers in your home is set apart for storing something specific. In the same way we can set aside storage places in the computer's memory to store something specific. In other words, we can create variables to store specific information such as a name, an age, a phone number, etc. There are two main types of variables: Numeric Variables and String Variables.

Numeric Variables

A Numeric Variable is a storage place for storing a numeric number. It can either be an integer or floating-point number. To store a numeric number into a Numeric Variable we use the "equal sign" (=). For example, to store the number 7 into a variable which we will create called Number you would use the following code:

Number = 7

The above line of code is called an Assignment Statement because it assigns a value to a variable. The number 7 is now stored in the variable Number. Whenever we want to access the contents of that variable we just need to use its name. For example, to print the contents of a variable to a screen we could use the following code:

print Number

Let's see how this looks in Liberty BASIC:


In Liberty BASIC variable names are displayed in a teal (blue-green) colour. You will notice that the first line that we have in our code window simply stores the number 7 into the variable Number. Nothing is displayed in the run window from this line because variable assignments, like mathematical expressions, are done "behind the scenes" by the computer and are not meant to be seen by the user.

However, because we are using the PRINT statement on the second line of our code, this line does display something in our run window. The second line prints the contents of what is stored in the variable Number. Really all variable names like the name Number here point to a specific location in memory. When the computer sees a variable name it knows that it needs to look in that memory location to retrieve what is actually stored there. In our example, we are printing the contents of the memory location that the variable Number points to. In this case the contents in that particular memory location is 7. The computer retrieves the number 7 and displays it to our run window.

We can also store the result of a mathematical equation in a variable. For example, the code

Total = 5 + 4 + 7

would store the value 16 into the memory location pointed to by the variable Total. In a similar way we can store all three of these numbers into different variables and use the variables in our mathematical expression.

Number1 = 5
Number2 = 4
Number3 = 7
Total = Number1 + Number2 + Number3


In this code we first assign the number 5 to Number1, 4 to Number2, and 7 to Number3. Then we use a mathematical expression and assign the total of all three numbers to the variable Total. Again, in both of these examples nothing is displayed to the run window because we haven't told it to print anything. To print our total we might have a print statement at the end of our code that looks like this:

print "The total is "; Total

String Variables

String variables, as you probably guessed, are for storing strings. String variables look the same as numeric variables except for the $ at the end of the name. For example, to store someone's name into a string variable you might make a variable called Name$ as in the following example:

Name$ = "Rob Sentis"
print "Welcome to the program "; Name$


This would produce the following run window:


Notice that the program first stores the name into the variable Name$. The next line then prints out the statement to the run window and replaces the variable Name$ with the contents that are stored in it. Notice also that we forgot to put a period at the end of a sentence. Since most programs are created for professional purposes we need to make sure that every detail looks good - even right down to the punctuation of sentences. We could change our last bit of code so that it looks like this:

print "Welcome to the program "; Name$; "."

This will now add a period to the end of our sentence.

Variable Names

Variable names can be whatever you want with a few exceptions. However it is a good idea to use meaningful names. For example, if someone was looking at your code and saw:

x = 2002
Number = 17
Print "You were born in "; x - Number


they might be able to guess that 2002 represents a year but may not know much more than that. Therefore, instead of calling your variable x or Number, you should usually call it something more meaningful as in the following example:

CurrentYear = 2002
Age = 17
Print "You were born in "; CurrentYear - Age


Now anyone looking at your code can more easily read what it does. In fact, it even helps you as a programmer keep track of what everything is doing.

Here are some requirements that Liberty BASIC requires for variable names:

Variable Name Requirements
A variable name must begin with a letter (a to z or A to Z).
The remaining characters of the variable name can be letters, numbers, or periods.
Variable names are case sensitive which means that Number, number, NUMBER, and NuMbEr are all different variables.
Variable names cannot be a Liberty BASIC reserved word. (i.e. a variable name cannot be PRINT etc.)
String variables must have a dollar sign ($) at the end of the name.