Using Constants
Variables can change values while the program’s running; that’s why they’re called variables (because their values can vary). Sometimes, you may want to use a fixed value throughout your program. Look at the following program. Can you figure out what the number 0.1975 stands for?
Balance = 43090
OurProfit = Balance * 0.1975
Balance = Balance + OurProfit
PRINT “Pay this amount, or we’ll get you = “; Balance
PRINT “Today’s current loan sharking interest rate = “;
.1975
END
A quick glance at the preceding program shows that the meaning of the number 0.1975 isn’t obvious. Because the meaning of numbers isn’t always clear without additional explanation, programmers use constants. A constant is nothing more than a descriptive name that represents a fixed value.
Remember Older versions of the BASIC programming language, such as Liberty BASIC, don’t support constants. Newer versions of BASIC (such as REALbasic) do support constants.
To see how constants can be helpful, look over the following REALbasic program:
Dim Balance, OurProfit As Single
Const InterestRate = 0.1975
Balance = 43090
OurProfit = Balance * InterestRate
Balance = Balance + OurProfit
MsgBox “Pay this amount = $” + str(Balance)
By reading this program, you can clearly see that the number 0.1975 represents an interest rate, which then makes the fourth line much easier to understand:
OurProfit = Balance * InterestRate
In this line, the value of the OurProfit variable is calculated by multiplying the variable Balance by the constant value of InterestRate.
By replacing a descriptive name (constant) to represent a fixed value, you can make the meaning of numbers easier to understand. Although the preceding REALbasic program is fairly small, imagine a program that consists of a million lines of code and uses the value of 0.1975 (as the interest rate) 20 times. What happens if you want to change the interest rate from 0.1975 to 0.2455? You have to change it all 20 times where it appears, which means you have to take the time to find it and change it all 20 times (while hoping that the value of 0.1975 isn’t also used somewhere else that you don’t know about).
Rather than go through this hassle, constants let you assign a fixed value to a descriptive name and then use that descriptive name throughout your program. If you need to change the value of that constant, you just need to change it once and its value automatically changes throughout the rest of your program.
-
Constants offer two key advantages:
-
Descriptive constant names make fixed values easier to understand.
-
Storing a value as a constant lets you update that value just once no matter how large your program is.
Technical Stuff To define a constant in the C++ programming language, you need to use the const keyword followed by the data type, the constant name, and the value to define the constant, as shown here:
const double interestrate = 0.1975;
This C++ code tells the computer, “Create a constant that can represent a double-precision number (decimal number), name the constant interestrate, and set the value of the constant to 0.1975.”
To define a constant in Revolution, you just have to use the constant keyword followed by the constant name and its value:
constant InterestRate = 0.1975
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