Some of the data used within a program is expected to change every time the program runs. Data like this is stored in the main memory of the computer in storage locations called variables. Examples of such data are: a person's name, the price of a product, an object's weight, etc.
As a program is being developed, the analyst will document the use of variables and their identifiers (labels) in a formal Variable List such as the one below.
| IDENTIFIER | DESCRIPTION | DATA TYPE | SOURCE | USAGE | DESTINATION |
|---|---|---|---|---|---|
| PRICE | Price of product in dollars | Floating Point | Keyboard | TAX & PAYMENT | Printer |
| TAX | State Sales tax (in dollars) | Floating Point | Calculated | for PAYMENT | Printer |
| PAYMENT | Total payment (in dollars) | Floating Point | Calculated | --- | Printer & Screen |
The analyst normally starts by filling in the DESCRIPTION column which clearly identifies each piece of data that must be stored including units (such as dollars). At the same time, each variable's data type is noted. Then an identifier is selected for each variable and noted in the first column to use as a shorthand notation in subsequent documentation. The last three columns are used to indicate: SOURCE - where the data comes from, USAGE - what happens to it while it is stored, and DESTINATION - where it will end up (for example, a screen, a printer, or disk storage). Notice that each variable always has an entry in the SOURCE column (all data comes from somewhere) and each variable always has an entry in at least one of the other two columns as well. Some variables have an entry in all three columns.
Programmers prepare a program to store variable data by declaring idntifiers for each variable in the source code ahead of the instructions that will use them. The declaring statement also must identify the type of data so that the program will reserve sufficient storage space for the data and store it in an appropriate data language. For more information about data languages, read the web page entitled Data Types in the C++ Programming Language on this web site. If you wanted to store the price of a product in dollars, you would want to use a data type (language) that could represent floating point (values with fractional portions, such as 2.58). In the C++ programming language, this data type can be declared as either float data (for floating point numbers with only a few digits) or double data (for large or precise numbers). The statement used in C++ to declare a floating point variable with an identifier of PRICE would be either:
float PRICE;
or
double PRICE;
Once the identifier PRICE is properly declared in this manner, the programmer can then refer to the data within the variable by using its identifier in the program's instructions. When the program's source code is later translated into machine language, the compiler will substitute the numeric address of the PRICE storage location in place of the label in each instruction that refers to that label. Thus, variable identifiers do not exist in machine language; variables' addresses are used instead.
The labels chosen by programmers for naming variables, named constants, symbolic constants, and other program objects are called identifiers. Each programming language has rules of syntax related to naming identifiers. In C++, the rules are simialr to those in other languages:
Try to keep identifiers short for efficiency, but long enough to be "mnemonic" (easy to remember). Blanks spaces are not allowed in an identifier, but you can use the underscore character in the same manner as a space for readability. For example, the label LAST_NAME is easier to read than LASTNAME. Capitalization can also be used to improve readability, but must be used consistently throughout a program. An identifier declared using the label LastName must be written exactly that way everywhere in a program. Use of a capital letter in the middle of an identifier is referred to as using the "camelCase". The labels LASTNAME, LastName, and lastname would identify 3 different objects in the C++ language.