
Before we can work with variables, we have to understand what they are. A variable is simply a piece of memory that has a name and a value. The name and the memory it is located in will always stay the same, however, the value can change. This will come clearer as we use examples.
The most basic types of variables in TI-BASIC are the letters A-Z. Stroing values into variables is easy, and can even be done outside the scope of a program in your own calculations. The easiest way to store a value into a variable is by the following syntax:
value STO-> variable
An example of storing a value of ten into the variable W is:
10 STO-> ALPHA W
If you're wondering where the store key is, it's located directly above the ON key. *NOTE - When you press STO-> only the -> will be visible.
By now you're probably wondering how this could be useful at all. The reason variables are useful is because they store data that you feel you will need to call upon later. Variables can be manipulated just like real numbers. So, say that later in your program you need to add the value of W with 5 and store it into T, you could do this quite legally:
W + 5 STO-> T
Now T will be equal to 15, and W is still equal to 10. You can perform all math operations on variables that you can on regular numbers. You can square them, subtract them, divide them, whatever. You can even work with just soley variables. For instance:
W + T STO->R
Variables can be displayed to the screen using the Disp command that we talked discussed in the last tutorial. However, this time when we call Disp, since we are not using text, we will not need the quotes. To illustrate the full effects of everything that I have discussed in this tutorial, I will give you the full source for a program.
Create a new program and call it DISP. Then put in the following code:
:ClrHome
:10 STO-> W
:W + 5 STO-> T
:W / T STO-> R
:Disp W
:Disp T
:Disp R
When you run this program, you should see: 10, 15, and .6666667. Now you know how to apply this. Try writing a simple program that does something to the extent of this. In the next tutorial, we'll cover how to get user input, which will make variables very meaningful.
The source files for this lesson can be downloaded here.