Thursday, September 27, 2007

Defining Variables

Defining Variables
Now that we are beyond our first program, we are going to want to manipulate some meaningful information and get some answers. An essential element in this process is having a piece of memory that we can call our own, that we can refer to using a meaningful name and where we can store an item of data. Each individual piece of memory so specified is called a variable.
Each variable will store a particular kind of data, which is fixed when we define the variable in our program. One variable might store whole numbers (that is, integers), in which case it couldn't be used to store numbers with fractional values. The value that each variable contains at any point is determined by the instructions in our program and, of course, its value will usually change many times as the program calculation progresses.
Let's look first at the rules for naming a variable when we introduce it into a program.
Naming Variables
The name we give to a variable is called an identifier, or more conveniently, a variable name. Variable names can include the letters A-z (upper or lower case), the digits 0-9 and the underscore character. All other characters are illegal. Variable names must also begin with either a letter or an underscore. Names are usually chosen to indicate the kind of information to be stored.
In Visual C++, variable names can be up to 247 characters long, which gives you a reasonable amount of flexibility. In fact, as well as variables, there are quite a few other things that have names in C++. We shall see they too can have names of up to 247 characters, with the same definition rules as a variable name. Using names of the maximum length can make your programs a little difficult to read and, unless you have amazing keyboard skills, they are the very devil to type in. A more serious consideration is that not all compilers support such long names. If you anticipate compiling your code in other environments, names with up to 31 characters are usually adequate and will not cause problems in most instances.
Although you can use variable names that begin with an underscore, for example _this and _that, this is best avoided, because there are potential clashes with standard system variables which have the same form. You should also avoid using names starting with a double underscore for the same reason.
Examples of good variable names are:
Price
discount
pShape
Value_
COUNT
8_Ball, 7Up, and 6_pack are not legal. Neither is Hash! or Mary-Ann. This last example is a common mistake, although Mary_Ann would be quite acceptable. Of course, Mary Ann would not be, because blanks are not allowed in variable names. Note that the variable names republican and Republican are quite different, as upper- and lower-case letters are differentiated.
Keywords in C++
There are reserved words in C++, also called keywords, which have special significance within the language. They will be highlighted with a particular color by the Visual C++ editor as you enter your program. If the keywords you type do not appear highlighted, then the keyword has been entered incorrectly.
Remember that keywords, like the rest of the C++ language, are case-sensitive.
For example, the program that you entered earlier in the chapter contained the keywords int and return. You will see many more as you progress through the book. You must ensure that the names you choose for entities in your program, such as variables, are not the same as any of the keywords in C++. You can find a complete list of the C++ keywords in the online help, if you look under Visual C++ Documentation \ Reference \ C/C++ Language and C++ Libraries \ C++ Language Reference \ Lexical Conventions \ C++ Keywords.
Declaring Variables
A variable declaration is a program statement which specifies the name of a variable and the sort of data that it can store. For example, the statement,int value;
declares a variable with the name value that can store integers. The type of data that can be stored in the variable value is specified by the keyword int. Because int is a keyword, you can't use int as a name for one of your variables.
Note that a declaration always ends with a semicolon.
A single declaration can specify the names of several variables but, as we have said, it is generally better to declare variables in individual statements, one per line. We will deviate from this from time to time, but only in the interests of keeping the code reasonably compact.
A variable name alone can't store anything, so it's not much use on its own. In order to store data (for example, the value of an integer), we need to assign a piece of the computer's memory to the variable. This process is called variable definition.
In C++, a variable declaration is also a definition (except in a few special cases, which we shall come across during the book). In the course of a single statement, we introduce the variable name, and also tie it to an appropriately-sized piece of memory. So, the statement int value;
is both a declaration and a definition. We use the variable name value that we declared, to access the piece of the computer's memory that we defined.
We use the term declaration when we introduce a name to our program, with information on what the name will be used for. The term definition refers to the allotment of computer memory to the name. In the case of variables, we can declare and define in a single statement, as in the line above.
You must declare a variable at some point between the beginning of your program and when the variable is used for the first time. In C++, it is good practice to declare variables close to their first point of use.
Initial Values for Variables
When you declare a variable, you can also assign an initial value to it. A variable declaration that assigns an initial value to a variable is called an initialization. To initialize a variable when you declare it, you just need to write an equals sign followed by the initializing value after the variable name. We can write the following statements to give each of the variables an initial value:int value = 0;
int count = 10;
int number = 5;
In this case, value will have the value 0, count will have the value 10 and number will have the value 5. These three statements are each declarations, definitions and initializations.
There is another way of writing the initial value for a variable in C++ called functional notation. Instead of an equals sign and the value, you can simply write the value in parentheses following the variable name. So we could rewrite the previous declarations as:int value(0);
int count(10);
int number(5);
If you don't supply an initial value for a variable, then it will usually contain whatever garbage was left in the memory location it occupies by the previous program you ran (there is an exception to this which we shall see later). Wherever possible, you should initialize your variables when you declare them. If your variables start out with known values, it makes it easier to work out what is happening when things go wrong. And if there's one thing you can be sure of, it's that things will go wrong.

No comments: