Computer programs typically have one of two general purposes—to produce or identity a sought after value or to control some activity. To do so, data will need to be identified and processed. In most programming languages, data values will most often be referred through the use of variables. A variable can be thought of has the name for some value, e.g.,
You probably know of the word variable from your study of mathematics. Be aware. Beware! The meaning of "variable" differs in mathematics and computer science. In mathematics, a variable has a single, particular value in all aspects of the the problem being worked. It may be that the value of the variable is unknown. You might use the same variable for some different purpose in another problem in which case it will have a different value, but again, that value will be the same throughout the problem.
In programming, a variable is something like scratch paper where you write a value down so you can remember it. And variables, truly vary depending on the code in the program. At one point in the program the variable may have no value—haven't written anything on the scratch paper yet. At a later or different point in the program, it will likely have some value assigned to it—value noted on the scratch paper. At yet another point in the program the variable can have some other value assigned—value on scratch paper crossed out and a new value noted. This might happen repeatedly during the course of the program.
It is critical that you recognize how variables work in programming. Before explicitly being given a value, a variable should be thought of as having no value or being undefined. Once it is explicitly given a value it retains that value until it is explicitly given another (probably different) value. Variables are given values in an assignment statement. The assignment statement, however, only gives the variable a value. It does not define how the variable's value is determined.
Assignment statements have the form
variable = expression
The name for the variable starts the statement and is followed by the assignment operator (an equal sign in our language). After the assignment operator comes an expression. The expression can be anything that results in a value, e.g.,
weight / (height ^ 2 * 703),
Pi() * radius ^ 2
For example if the variable
grossPay is given the value of
payRate * hoursWorked it retains that value, even if the values of
hoursWorked change. The following commented code illustrate this.
' declare three variables -- all are undefined Dim grossPay, payRate, hoursWorked as Double ' get a value from user for payRate (perhaps 15.00) payRate = InputBox("Hourly pay rate?") ' get a value from user for hoursWorked (perhaps 40) hoursWorked = InputBox("Hours worked?") ' calculate and store the value in the variable grossPay (600.00 in this case) grossPay = payRate * hoursWorked ' report the result ("Gross pay is $600.00" in this case) MessageBox.Show("Gross pay is " & grossPay.ToString("c")) ' get (and store) a new value for payRate payRate = InputBox("Hourly pay rate?") ' NOTE THAT THE VALUE FOR grossPay has not changed
Most people do not commonly use variables in their daily activities and, therefore, have some difficulty understanding variables and thinking about problems in terms of variables. That makes programming more challenging because programming almost always includes getting data (and storing it in variables), processing data (by manipulating variables and, perhaps, storing results), and reporting those results. Eventually, thinking using variables becomes second nature. It may be helpful to consider how variables are used in computer programs—the roles they can play.
The ideas here are adapted from the Roles of Variables website managed by Jorma Sajaniemi. This discussion applies to individual variables not array or other collections. Also, the role names/labels are somewhat different (but the roles are mostly the same).
As you read about the roles, try to imagine an example or problem of your own that would make use of a variable in that role. Variables represent
Multiple values for the same entity will be encountered or produced. The variable is/contains the most recent or current one. Some examples are:
A single value for the entity is expected. The value, once received, is unlikely to change in the current execution of the program. Some examples are:
The variable is used to aid in accomplishing a particular task but once the task is complete the variable is no longer needed. Some task examples are:
temp = val1 val1 = val2 val2 = temp
We often need a variable that counts for us (perhaps by twos, fives, or tens) or otherwise changes in an orderly manner. Some examples are:
An aggregator collects or gathers together some value(s). Often this is a sum, but it could be a count of occurrences of some value or activity.
Often programs are looking for the largest or smallest value that are encountered. A variable is used to store that value. Typically, the variable is initialized to an opposite extreme value. For instance, if you are looking for the highest temperature, you would set the
highestTemp variable to a very low value so that whatever values you encounter will be greater.
In some problems it is necessary to keep track of a previous value so you will be able to decide what action to take. For example, if you were keeping track of the number of times a coin flips in a row were the same you would need to know about the last flip. Also, some calculations depend on the prior calculation or two (e.g., Fibonacci numbers).
A flag value indicates a program state (often a Boolean value). In a craps game program you have to know whether the shooter is rolling in the come out phase to set the point or in the point phase to try to roll the point. A variable such as
inComeOut could be set to indicate the state of the program.
(This was not in the original set of roles. I thought the current value and fixed value roles might be too inflexible so add this one.)