The first important concept in programming languages is the idea of identifiers. Identifiers are descriptive names that are mapped to locations in the computer's memory. Once a memory location is given a specific name, we can refer to that location using the identifier rather than the numeric address. This system of associating a name with a memory location allows us to choose names that give meaning to the contents of the memory location.
For example, suppose you wanted to write a simple program to calculate the amount of sales tax you will need to pay on a new pair of shoes that costs $19.95. The first thing you will need to do is create an identifier called PriceOfShoes that is mapped to a particular memory location. Then you need to represent the value 19.95 in the computer at that location. Once this is done, you can use your identifier PriceOfShoes to refer to the price of the shoes in your program.
To be precise, we need to remember that most computers use the binary system for their storage and arithmetic rather than the decimal system. This means that when we associate a fractional value such as 19/20 (or 0.95) with a variable, we are actually storing a representation of 19/20 rather than the exact value. Since the binary number system has no exact representation for many fractions like 19/20, 1/3, or even 1/10, the computer must use a close approximation to represent this value. While we often think of exact values being stored in the computer's memory, the reality is that only close approximations for some numbers can be stored. However, to simplify our discussion, we will refer to exact values as being stored in memory although we know that only representations are really stored in most cases.
What happens if you need to compute the sales tax on another pair of shoes with a different price? Do you need to create another identifier? Actually, all you need to do is change the value that is stored in the memory location named PriceOfShoes. You can use the same memory location and identifier but vary the value that is stored. Memory locations that are used in the fashion are known as variables. Be careful not to confuse variables and identifiers. Identifiers are only the names given to variables, but variables are the actual memory locations used to store data. You can compare this with cities on a map. Cities represent a specific location on a map just like variables represent a specific location in memory. But when we refer to a city, we use a name like Richmond to identify the city. In the same way, when we refer to a memory location, we use an identifier like PriceOfShoes to name the variable. The contents of our variable can change just like the residents of a city can change. But although the contents change, the name or identifier remains the same.
We can also use identifiers to name other things in programming languages beside variables. Another important use for identifiers is giving names to values that remain the same throughout a program. These values are called constants. A good example of a value that can be used as a constant is sales tax. In Virginia, the sales tax is 4.5% so we can represent this by creating a constant called TAXRATE and associating it with the value 0.045. Now whenever we need the value for the sales tax, we can use the name TAXRATE to refer to it.
You might be wondering why we even bother to give a name to the value for sales tax. Why not simply use the value 0.045? The answer is that by using constants, we can easily modify our program to run under different conditions. For example, suppose a friend who lives in Tennessee wanted to use your program to compute sales tax. Since the sales tax in Tennessee is 6%, he will need to change all the sales tax computations to use 0.06 instead of 0.045. This modification is very easy if the program uses the constant TAXRATE. All he needs to do is associate the identifier TAXRATE with the new value of 0.06. Another advantage of using constants is that they make our programs more readable. The identifier TAXRATE is much more readily understood than the number 0.06.
As we stated before, both variables and constants are named using identifiers. However, the purpose of variables and constants is very different. The table below compares variables and constants to highlight their different roles.
One last note about identifiers: In most programming languages, identifiers are required to conform to a certain format. For example, the identifiers in this lesson all began with letters and were composed only of letters and numbers. None of the identifiers included spaces, and constants were written in UPPERCASE letters. This is a typical format for identifiers, and we will use this format in the rest of the lessons.