Variables (Basic Information)

J. Philip East


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., age, height, payRate, count, total, average, etc.

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.,

For example if the variable grossPay is given the value of payRate * hoursWorked it retains that value, even if the values of payRate and 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

Roles of Variables

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

Types of Data (Variables)

Accessing Data in Variables