Defining the data type of a variable
Programming languages like Liberty BASIC and Revolution let you create a variable and stuff it with data right away. This makes programming easy because you can create variables whenever you need them. Unfortunately, it also encourages sloppy programming for two reasons:
-
You have to search your code, line by line, to find all the different variables your program creates and what type of data it stores in them.
-
You can easily store the wrong type of data in a variable by mistake.
Consider the following Liberty BASIC code. The code works but doesn’t make any logical sense because nobody can have 1.48 numbers of children.
Children = 1.48
MonthlyPayment = 350
PRINT “You owe = “; Children * MonthlyPayment
END
In this example, the Children variable should hold only whole numbers and not decimal numbers.
To avoid storing inappropriate data in a variable, many programming languages do something called type-checking. Type-checking forces you to define the type of data a variable can hold, and then it checks to make sure you don’t try to store the wrong type of data in that variable. In this way, type-checking acts like a filter that allows only valid data into the variable.
Technical Stuff Programming languages that force you to define the type of data a variable can hold are also known as type-safe languages.
Warning! If you don’t define what type of data a variable can hold, it’s possible to store the wrong type of data in a variable. If your program tries to work with the wrong type of data, it could crash or just mess up very badly.
In type-safe languages such as REALbasic and C++, you must define both the name of a variable and the type of data it can hold. Only after you have defined (or declared) both the variable name and its data type can you stuff data in that variable.
The three most common types of data that variables can hold are
-
Whole numbers (such as 3, 45, or 102): Often referred to as integers.
-
Decimal numbers (such as 34.04679, 3.1415, or 26.943): Often referred to as single or double to represent single-precision or double-precision numbers. Single- or double-precision refers to how many decimal places the computer uses to store a number. Double-precision numbers can store more decimal places than single-precision numbers.
-
Text (such as “a”, “my cat”, “fat”, or “I like frogs”): Often referred to as strings.
The three most common data types are integers, decimal numbers, and strings. However, some programming languages offer other data types such as Boolean (which can only hold a True or False value), byte (which is a subset of an integer but can hold only values ranging from 0 to 255), and long (which can hold extremely large or small integer values).
The main reason for these other data types is to give programmers the freedom to choose the data type that most closely fits the acceptable range of values a program needs to store in a variable. For example, an integer variable type can hold both positive and negative numbers, but a byte data type can hold only values ranging from 0 to 255. So if you need to create a variable to hold someone’s age, you should declare your variable as a byte because a person’s age can never be negative nor higher than 255. Additional data types simply help you further restrict what types of values a variable can hold.
The following REALbasic code example shows how to declare a variable named Children and define it to hold only whole numbers (also called integers).
Dim Children As Integer
Dissecting this REALbasic code reveals the following:
-
The Dim command is short for dimension, which is just a fancy way of saying “I declare a variable…”
-
Immediately following the Dim command is the name of the variable, which is Children. So the first two words in the REALbasic code tell the computer, “I declare a variable named Children….”
-
The last two words, As Integer, define the data type as an integer. So the entire REALbasic code tells the computer, “I declare a variable named Children as an Integer data type.” At this point, the Children variable can hold only integers.
Declaring a variable named Children and defining it to hold only integer data in C++ might look like this:
int Children;
Dissecting the preceding C++ code reveals the following:
-
The int command is short for integer and tells the computer, “I want to create a variable that can hold only integer values….”
-
Children is the actual variable name, so the first two lines tell the computer, “I want to create a variable that can hold only integer values, and the name of the variable is Children.”
-
The semicolon tells the computer, “That’s the end of my variable declaration.”
Remember C++ commands tend to be much shorter (and harder to read) than equivalent BASIC commands. C++ resembles shorthand whereas BASIC resembles actual words.
If you declare a variable to hold only integers, you can never accidentally store decimal numbers (3.1415) or text (twenty-five thousand) in the variable by mistake.
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a Reply