Chapter II.2. Variables, Data Types, and Constants

Every program consists of a list of instructions that tell the computer what to do. The simplest program consists of a single instruction, such as one that tells the computer to display the words, Hello, world! on-screen.

Of course, any program that does the same thing over and over again isn't very useful or interesting. What makes a program useful is when it can accept information from the outside world and then respond to that information.

So instead of just displaying Hello, world! on-screen, a more useful program might ask for the user to type his name in so the program could display Hello, Bob!

Programs don't always have to get information from a person. Sometimes, programs can retrieve data that's stored somewhere else, such as a list of employees stored on another computer.

That program could access this database over a network and determine which person has been assigned to which computer in the building. Then the program can retrieve each person's name so when he turns on the computer, the program displays his name on his screen.

An even more sophisticated program could work with a Web cam hooked up to the computer along with a database that includes employee names and their photographs. So every time any computer's Web cam spots someone sitting at the computer, the program could examine the person's image through the Web cam, compare that image to the photographs of all employees stored in the database, and then find the name that matches the person. Then the program could display that person's name on-screen.

To be useful, every program needs to retrieve and respond to data from an outside source whether it comes from a person, a device such as a Web cam, or even another computer program. Where the data comes from is less important than what the program does with the data after the program receives it.

Declaring Variables

If somebody handed you a $20 bill, you could put it in your pocket. When someone hands a program some data, the program also needs a place to put that data. Programs don't have pockets to store stuff, so they store stuff in variables.

Just as your pockets can hold money, rocks, or dead frogs, so can a variable hold different types of data, such as numbers or words. The contents of a variable can vary, which is why they're dubbed variables.

Warning

The whole purpose of variables is to make a program more flexible. Rather than behave the same way using identical data, programs can retrieve, store, and respond to data from the outside world.

Creating a variable

You can't shove anything in your pockets until you have a pocket. Likewise, you can't shove any data in a variable until you first create that variable. To create a variable, you must first declare that variable.

Declaring a variable tells the computer, "I need a pocket to store data." When you declare a variable, the computer carves up a chunk of its memory for your program to use for storing data.

Of course, you can't just dump data in a variable without knowing how to find it again. In the real world, you can find something by remembering whether you stored it in the left or right pocket. In a program, you can remember where you stored data by giving a variable a unique name. When you create a variable, you declare that variable's name at the same time as the following BASIC code demonstrates:

X = 9

This BASIC code tells the computer the following:

  1. Create a variable.

  2. Give that variable the name X.

  3. Store the number 9 in the variable named X.

Variable naming conventions

Variable names are for your convenience only; the computer doesn't care what names you choose for your variables. Computers are perfectly happy using generic variable names, like X or Y, but the more descriptive the name, the easier it is for you (or another programmer) to understand what type of data the variable holds.

For example, looking at the earlier BASIC code of X = 9, can you tell what the X or the number 9 represents? Now look at the same code, but using a descriptive variable name:

BaseballPlayers = 9

By just changing the variable X to a more descriptive BaseballPlayers name, you can guess that the number 9 refers to the number of players on a baseball team.

Variable names can be as simple as a single word, such as Age, Name, or Salary. However, a single word may not adequately describe the contents of some variables, so programmers often use two or more words for their variable names.

This is where different programmers use different styles. Some programmers like to cram multiple words into one long variable name like this:

salestax

For two words, this can be acceptable, but when creating variable names out of three or more words, this can start getting messy, such as

salestaxfor2008

To help identify the separate words that make up a long variable name, some programmers use the underscore character, such as

sales_tax_for_2008

Other programmers prefer to use uppercase letters at the beginning of each new word, such as

SalesTaxFor2008;

You can always mix and match both conventions if you want, such as

SalesTaxFor_2008;

No matter which naming style you prefer, it's best to stick with one style to prevent your program from looking too confusing with so many different variable naming styles all over the place.

Every variable needs a unique name. If you try to give two variables identical names, the computer gets confused and refuses to run your program.

Warning

In some languages, such as the curly bracket family, which includes C, C++, and Java, variable names are case-sensitive. Therefore, a variable named salestax is considered completely different than a SalesTax variable.

Creating variables in a command

The simplest way to create a variable is when you need it in a command. Suppose you have a program that asks the user for his annual salary, such as the following command (written in the Python programming language):

salary = input("What is your annual salary?")

This command displays, "What is your annual salary?" on-screen and waits for the user to type in a number.

As soon as the user types in a number, such as 20000, the program needs a variable to store this number. In this example, the program creates a salary variable and stores the number 20000 in that variable.

Creating variables whenever you need them in a command is simple and convenient — and potentially troublesome if you aren't careful. The biggest problem is that when you create a variable within a command, that variable can store any type of data.

Variables can hold two types of data:

  • Numbers, which are typically used to represent quantities or measurements.

  • Text (sometimes called strings, as in "text strings") is used to represent non-numeric data, such as names or mailing addresses.

You can always perform mathematical calculations on numbers, but never on text. So if a program expects to store a number in a variable, it gets confused if it receives text instead. The following Python program works perfectly just as long as the user types in a number.

salary = input("What is your annual salary?")
taxrate = 0.30
print "This is how much you owe in taxes = ", salary * taxrate

This program works as follows:

Line 1: The program displays, "What is your annual salary?" Then it waits for the user to type in a number. If the user types in 20000, the program stores the number 20000 in the salary variable.

Line 2: Store the number 0.30 in the taxrate variable.

Line 3: Multiply the number in the salary variable by the number in the taxrate variable and print out the result. If the salary variable holds the number 20000, the program prints, "This is how much you owe in taxes = 6000.0".

Instead of typing the number 20000, what if the user types in 20,000? Because the program doesn't recognize 20,000 as a valid number (because of the comma), the program treats 20,000 as text that's no different than twenty thousand. Trying to multiply the number 0.30 by the text 20,000 doesn't make sense, so the program stops running, as shown in Figure 2-1.

If a program tries to store the wrong type of data in a variable, the program stops running.

Figure II.2-1. If a program tries to store the wrong type of data in a variable, the program stops running.

Declaring the data type of a variable

You can never stop users from typing in the wrong type of data, such as typing in the words twenty thousand instead of the number 20000. However, you can protect yourself from trying to do something wrong, like multiplying a number by text. In the Python programming language, the following program actually runs:

salary = input("What is your annual salary?")
taxrate = "Thirty percent"
print "This is how much you owe in taxes = ", salary *
   taxrate

In this program, the third line tries to multiple the salary variable by the taxrate variable. If the program asks the user, "What is your annual salary?", and the user types 20000, the third line of the program tries to multiply 20000 (stored in the salary variable) by Thirty percent. Obviously, you can't multiply a number by a word, so this program would appear to work but prints only:

This is how much you owe in taxes =

The problem occurs because the program multiplies the salary variable by the text Thirty percent. Even though the program appears to run, it doesn't run correctly.

If you're writing a small program, you could examine your program, line-byline, to see where the problem might be, but in a large program that might consist of thousands of lines, trying to track down this simple problem could be time-consuming and frustrating.

To avoid this problem, many programming languages force you to define (declare) your variable names plus the type of data they can hold. By doing this, you can have the compiler check your program to make sure you aren't mixing data types accidentally, such as trying to multiply a number by text.

If your program does try to mix data types by multiplying a number by text, the compiler refuses to run your program, displays an error message, and highlights the problem so you can fix it right away, as shown in Figure 2-2.

You can't compile your program until the compiler is certain that you aren't mixing different data types.

Figure II.2-2. You can't compile your program until the compiler is certain that you aren't mixing different data types.

Using Different Data Types

Defining a specific data type makes it easy to

  • Know the type of data a variable can hold (either numbers or text).

  • Restrict the range of data the variable can hold.

  • Identify whether a variable can hold a number or text can keep your program from trying to add or multiply a number by a word, which doesn't work. Figure 2-3 shows the different categories of data types within most programming languages.

Different data types that can hold a range of values.

Figure II.2-3. Different data types that can hold a range of values.

Defining the range of data can prevent a variable from storing incorrect data. For example, if your program asks the user to type in his age, invalid data would include negative numbers, zero, and extremely large numbers, such as 259.

The range of numbers listed for Long, Single, and Double data types are listed as exponential numbers (that's the little e). So the Long data type can store a number as large as 2.147 with the decimal place moved nine places to the right or approximately 2,147,000,000.

Warning

Every programming language offers different data types, so use Figure 2-3 as a guideline rather than a strict reference.

For example, if you want to store a person's age in a variable, you probably want to store it as a whole number (such as 45) rather than a real number (such as 45.029).

Next, you want to choose a data type that contains the range of values a person's age might be. From Figure 2-3, you can see that a Boolean data type can only hold a 0 or 1. An Integer data type can hold negative numbers as small as −32,768 or positive numbers as large as 32,767. However, the range of an Integer data type is too large for a person's age. Nobody has a negative age and nobody has an age anywhere close to 32,767.

So the best data type to choose for storing a person's age is the Byte data type, which can store numbers from 0 to 255. If your program tries to store a number less than 0 or greater than 255 as a person's age, the Byte data type screams in protest and refuses to do it, a process known as type-checking.

Warning

Type-checking only makes sure that your program doesn't try to store incorrect data in a variable, but it can't protect against a clumsy user typing in his age as a negative number or a massively unrealistic number. To prevent user input error, programs must validate that the data is correct. If a person types 0 for his age, your program must refuse to accept the incorrect data and ask the user to try again. That means you must always write extra commands in your program to check for valid data that's received from the outside world.

Another reason to use different data types is to reduce the amount of memory your program needs. It's possible to declare an Age variable as either a Byte or Integer data type, as shown in the following Visual Basic code:

Dim Age as Byte

Or

Dim Age as Integer

Although both Age variables can store whole numbers, such as 39, the Byte data type uses less memory (1 byte) than the Integer data type (4 bytes).

Note

A byte is just a measurement unit where 1 byte represents the space needed to store a single character (such as B or 8). In comparison, a typical sentence requires 100 bytes of storage, a page in a book might require 10,000 bytes, and a novel might require 1 million bytes.

Bytes measure both storage space and memory so a Byte data type (storing the number 48) needs only 1 byte of space whereas an Integer data type (storing the same number 48) would need four times as much space. If you choose the wrong data type, your program still works, but it uses up more hard disk space or memory than necessary. Table 2-1 lists common storage requirements for different data types.

Table II.2-1. Typical Storage Requirements for Different Data Types

Data Type

Storage Size

Byte

1 byte

Boolean

2 bytes

Character

2 bytes

Integer

4 bytes

Single

4 bytes

Long

8 bytes

Double

8 bytes

String

10 bytes + (2 * string length)

(If you had a string consisting of three letters, that string would take up 10 bytes of storage space plus (2 multiplied by three letters or 10 + (2 * 3), which is 16 bytes.)

So when declaring variables as specific data types, you want to choose the data type that can

  • Hold a range of valid values.

  • Uses the least amount of space as possible.

Storing Data in a Variable

After you declare a variable, store data in that variable as the following C program demonstrates:

int age;
age = 15;

Line 1: Declares the age variable as an integer data type.

Line 2: Stuffs the number 15 into the age variable.

You can assign a fixed value (like 15) to a variable or any equation that creates a value, such as

taxes_owed = salary * 0.30;

If the value of the salary variable is 1000, this command multiplies 1000 (the salary variable's value) by 0.30, which is 300. The number 300 then gets stored in the taxes_owed variable.

When storing data in variables, make sure the variable is either empty or contains data you can afford to lose. Variables can hold only one item at a time, so storing a new value in a variable wipes out the old value, as shown in Figure 2-4 and in the following BASIC code:

Salary = 25000
Salary = 17000 + 500
PRINT "This is the value of the Salary variable = ", Salary
Assigning a new value to a variable wipes out the old value.

Figure II.2-4. Assigning a new value to a variable wipes out the old value.

If you store a value in a variable and then immediately store a second value in that same value, you wipe out the first value in that variable. Obviously, there's no point in storing data in a variable only to wipe it out later, so when you store a value in a variable, you'll eventually want to use that value again. (Otherwise, there's no point in storing that data in the first place.)

Retrieving Data from a Variable

After you store data in a variable, you can treat that variable exactly like a fixed value. In this first Python language example, the print command just prints, "This is when I plan on retiring = 65".

print "This is when I plan on retiring = ", 65

Replacing the fixed value (65) with a variable and assigning the value of 65 to that variable creates the following:

age = 65
print "This is when I plan on retiring = ", age

Line 1: Stores the value 65 into the age variable.

Line 2: Prints, "This is when I plan on retiring = ", takes the last value stored in the age variable (which is 65), and prints that value out so the entire message that appears on the screen is:

This is when I plan on retiring = 65

Besides using variables as if they're a fixed value, you can also assign the value of one variable to another, such as

first_number - 39
second_number = first_number + 6

Line 1: Stores the number 39 into the first_number variable.

Line 2: Adds the number 6 to the value in the first_number variable (39) and assigns this sum (45) into the second_number variable.

You can also modify a variable by itself, as shown in this example:

people = 5
people = people + 12

Line 1: Stores the value of 5 into the people variable.

Line 2: Tells the computer to do the following:

  1. Find the value stored in the people variable (5) and add it to the number 12 for a total of 17.

  2. Store the value of 17 into the people variable, wiping out the last value stored there (which was 5).

Essentially, variables are nothing more than values that can change while the program runs. In the preceding example, one moment the value of the people variable was 5, and the next moment the value of that same people variable was 17.

Using Constant Values

As a general rule, never use fixed values directly in your program. The reason for this is simple. Suppose you need to calculate the sales tax in three different places in your program. Near the top of your program, you might have a command like this:

Material_cost = Item_cost + (Item_Cost * 0.075)

Buried in the middle of your program, you might have another command like this:

Product_cost = Part_cost + (Part_Cost * 0.075)

Near the bottom of your program, you might have a third command like this:

Project_cost = Material_cost + Product_cost + (Material_cost
    + Product_cost) * 0.075

In all three commands, the number 0.075 represents the current sales tax (7.5 percent). What happens if the sales tax suddenly jumps to 8 percent? Now you have to go through your entire program, find the number 0.075, and replace it with the number 0.080.

Searching and replacing one value with another can be tedious, especially in a large program. As an alternative to using fixed values directly in your commands, you can use constants instead.

As the name implies, constants are like variables that hold only a single value that never changes. So you could define the value of your sales tax just once, as the following Visual Basic code shows:

Const sales_tax as Single = 0.075

This command tells the computer:

  • Use the Const keyword to create a sales_tax constant.

  • Define the sales_tax variable as a Single data type.

  • Store the value 0.075 into the sales_tax variable.

By using constants, you can eliminate fixed values in your commands and replace them with a constant instead, such as

Const sales_tax as Single = 0.075
Material_cost = Item_cost + (Item_Cost * sales_tax)
Product_cost = Part_cost + (Part_Cost * sales_tax)
Project_cost = Material_cost + Product_cost + (Material_cost
   + Product_cost) * sales_tax

Now if the sales tax changes from 0.075 to 0.080, you just need to change this value in one constant declaration, such as

Const sales_tax as Single = 0.080

This one change effectively plugs in the value of 0.080 everywhere your program uses the sales_tax constant. So constants offer two advantages:

  • They let you replace fixed values with descriptive constant names.

  • They let you change the value of a constant once and have those changes occur automatically in the rest of your program.

    So use constants to replace fixed values and use variables to store different types of data retrieved from the outside world. Every program needs to use variables, but not every program needs to use constants.

After you understand how to store data temporarily in variables, your program can start manipulating that data to do something useful.

Defining the Scope of a Variable

The scope of a variable defines which part of your program can store and retrieve data in a variable. Because variables store data that your program needs to work correctly, your program must make sure that no other part of the program accidentally modifies that data.

If your program stores a person's credit card number in a variable, you don't want another part of your program to accidentally retrieve that data and change the numbers around or send a hundred copies of each credit card number to customers outside the company.

So when creating variables, limit the variables' scope. The scope simply defines which parts of your program can access a variable. When you declare a variable, you also define one of three possible scope levels for that variable:

  • Global

  • Module

  • Subprogram

Handling global variables with care

In a global variable, any part of your program can access that variable, including storing new data in that variable (and wiping out any existing data already stored in that variable), changing the data in a variable, or wiping out the data in a variable altogether, as shown in Figure 2-5.

Every part of a program can access and change a global variable.

Figure II.2-5. Every part of a program can access and change a global variable.

Warning

Use global variables sparingly. If you create a global variable and some part of your program keeps modifying that variable's data by mistake, you have to search through your entire program to find which command is messing up that variable. If you have a million-line program, guess what? You have to examine a million lines of code to find the one line that's changing that variable by mistake. If that's your idea of fun, go ahead and use global variables.

In the old days, all programming languages let you create global variables, and it was up to the programmer to make sure no commands accidentally modified that variable in unintended ways. When writing small programs, programmers can do this easily, but when working on massive programs created by teams of programmers, the odds of abusing global variables increases dramatically.

Think of a shelf where you can store your books, wallet, and laptop computer. If you're the only person who has access to that shelf, you can be sure anything you put on that shelf is there when you look for it again.

Now imagine putting your shelf of personal belongings (books, wallet, and laptop computer) on a shelf located in Grand Central Station where thousands of people can grab anything they want off that shelf or put something else on there instead. Would you feel safe leaving your wallet and laptop on such a shelf? If not, you probably wouldn't feel safe putting your data in a global variable either.

Because global variables can be so dangerous, most programming languages don't let you create a global variable unless you specifically tell the computer to create one. To create a global variable, you often have to use a special keyword, such as global or public, such as

Global X : Integer

The preceding command tells the computer to create a global variable, called X, that can hold an integer data type. You type a global variable declaration in any file that contains your main program or a library of subprograms.

Restricting scope to a module

A module is another term for a separate file. If you divide a large program into subprograms and store those subprograms in a separate file, each file is a module. A module variable lets you create a variable that can be accessed only by code stored in that particular module, as shown in Figure 2-6.

The advantage of module variables is that they give you the convenience of a global variable but without the danger of letting every part of a program access that variable. Instead, module scope limits access only to code in that file.

Although an improvement over global variables, module variables also have some of the disadvantages of global variables. If a file contains a lot of subprograms, trying to find which line of code is messing up your module variable can still be troublesome.

To create a module variable, you generally just declare a variable at the top of any file except you don't use the Global or Public keyword. In some programming languages, you declare a module variable with the Private keyword, such as

Private X : integer;

The preceding code would declare a module variable called X, which can hold an integer data type.

Module variables restrict access only to code stored in a particular file.

Figure II.2-6. Module variables restrict access only to code stored in a particular file.

Isolating variables in a subprogram

Because global and module variables can be dangerous to use because any part of a program can change them, most programmers use global and module variables sparingly. Most of the time, programmers declare variables within a subprogram. Therefore, the only code that can access that variable is inside the subprogram itself, as shown in Figure 2-7.

To create a variable with subprogram scope, you have to declare your variable at the top of that particular subprogram. The subprogram effectively isolates that variable from any other part of your program.

Subprogram variables restrict access only to code stored in that particular subprogram.

Figure II.2-7. Subprogram variables restrict access only to code stored in that particular subprogram.

Passing data among subprograms

If one subprogram needs to use data stored in another subprogram, what's the solution? The easiest (and most dangerous) solution is to let multiple subprograms access that variable as a global or module variable.

The better solution is to isolate that variable as a subprogram variable and then share or pass that data to another subprogram. This means you have to declare two subprograms variables, one in each subprogram. Although cumbersome (now you know why programs create global or module variables instead), passing data from one variable to another, dubbed parameter passing, helps keeps the data isolated in only the subprograms that actually need to use that data, as shown in Figure 2-8.

To share data among subprograms, you have to pass that data from one subprogram to the other.

Figure II.2-8. To share data among subprograms, you have to pass that data from one subprogram to the other.

Tip

As a general rule, restrict the scope of your variables as small as possible. This makes sure that as few lines of code can access that variable and mess up your program.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset