PBASIC Programming/Variables and Constants< PBASIC Programming
Need for StorageEdit
When we write a program, it's always a good thing to be able to store data that we are using, and to manipulate that data. If we want to set aside some storage for a particular use, we need to tell the computer to save that space, and we need to give the space a name. A variable is a storage location, like a box or a shelf in real life, that has a name. Think about the common algebraic equation for a straight line:
Where y is a variable that can take values dependent on the values for the other parameters.
A variable is a storage location where we can hold values. A variable can be the size of one of our datatypes, such as a bit, a nibble, a byte, and a word. Once we define a variable, the size of it is fixed: If we define a byte, our variable cannot contain values over 255.
Defining a VariableEdit
To define a variable, we use the VAR keyword. First we need to give a name to our variable, and the name for the variable cannot be the same as any of the reserved words. Variable names must start with a letter, may contain letters or numbers, and are not case sensitive. Here are some examples:
MyVariable1 VAR Bit MyVariable2 VAR Nib MyVariable3 VAR Byte MyVariable4 VAR Word
We can give names to the individual parts of our variables. For instance, we know that a byte is made up of 8 bits, and if we want we can give each of these bits a name, if we want. Notice that bit numbers start at 0, not 1. Here are some examples:
MyVariable VAR Byte FirstBit VAR MyVariable.Bit0 FirstNib VAR MyVariable.Nib0 SecondNib VAR MyVariable.Nib1 FirstBitFirstNib VAR FirstNib.Bit0
Notice that the variable
FirstBit is the same exact thing as
If we have multiple data values that are related, and we don't want to give them all individual names, we can create an array. An array is a large variable that contains many "boxes", each with an address. The array has a name, and to access an individual box in the array, you need the name of the array and the address. Here are some examples:
MyArray VAR Byte(10)
This creates an array with 10 bytes. Each byte can still only contain numbers up to 255, and we cannot combine multiple bytes in our array to make larger numbers.
We can use aliases with array elements too:
MyArray VAR Byte(10) MyVariable VAR MyArray(0)
We can also make more complicated aliases, but we need to keep the box address at the end:
MyArray VAR Byte(10) MyVariable VAR MyArray.Nib1.Bit0(0)
Sometimes when we are doing calculations, we can define a thing called a constant to make things easier. Constants are just numbers, not variables, and once you set the value it cannot be changed. A common example of a constant that people use is π = 3.1415.... Instead of having to write out π to several decimal points each time we want to use it, we can simply define a constant value with the name "pi". To define a constant, we use the CON keyword. Here are a few examples:
Ten CON 10 ACouple CON 2 ABunch CON 1024
Constants are useful for a number of reasons:
- Makes mathematical equations easier to read.
- If we need to change a value, we only change the CON definition, and we dont need to change it everywhere in the code