# Variables and Values

## FIELDS OF STUDY

Software Development; Coding Techniques; Computer Science

## ABSTRACT

Computer programs use variables and values to refer to and manipulate information. The information stored in variables can be of different types, including numbers and strings of characters. Programs can perform mathematical, relational, and logical operations on the values stored in variables.

## PRINCIPAL TERMS

• character: a symbol that represents a letter, number, punctuation, space, break, or other mark used in written language.
• operation: an action that a computer program performs on specified data, such as arithmetic calculation of the data contained in two variables.
• string: in computer programming, a data type consisting of characters arranged in a specific sequence.
STORING AND ACCESSING DATA

Computer programs need to store and manipulate vast amounts of different types of information. Without the ability to store and manipulate information, programs could not accomplish most tasks for which they are designed. Programmers use variables to refer to and use this information effectively. Variables can be thought of as containers that computer programs use to store and reference information in memory. The term “variable” is used because the information stored in a variable can change, or vary, during program execution.

A variable stores values of a specified data type, such as integers, strings, or real numbers. Variables have names like Number, Date, Location, and Average. The data contained in each variable can be of one or more values, such as 7 and 400 for Number, 07/17/2014 and 10/09/2016 for Date, Beach and Field for Location, and 5.873 and 0.064 for Average.

The actual information or data referenced by a variable is called a “value.” Variables can store various data types, including integers, real numbers, float numbers (decimals), Boolean (either-or), and strings. A string is a sequence of one or more characters. Characters are the symbols used in written language including letters, numbers, punctuation marks, and blank spaces.

Computer programs can perform operations on the values stored in variables. Typical operations include math calculations, logical comparisons, and relational operations, such as comparing two variables to see if they are equivalent.

Before a variable can be used in code, it must be declared, or given a unique name and data type. The name should describe the kind of information contained in the variable, not the value. The data type defines the possible values that the variable might contain. For example, a variable might be declared as holding any integer value from –2,147,483,648 to 2,147,483,648. Sometimes variables are assigned a starting value. Other variables can be used as part of a variable's starting value, as long as their own values are already declared in the code.

IMPORTANT CONSIDERATIONS WITH VARIABLES

Variables enable programmers to work with efficiently with large amounts of data of different types. However, potential errors can occur if variables are used incorrectly. To avoid these problems, programmers must consider a variable's scope and data type.

As variables are referenced by name, confusion and errors can result if two variables with the same name are used in unrelated sections of a program. Scope is used to address this issue. Scope defines the sections of a program that have access to a variable. For example, a variable used to store a counter used by all sections of a program would be defined as having a global scope, making the variable available to code throughout the program. A different variable that is used only in one function could be defined as having a local scope and would be available only to code within that process. Using scope to control access to variables prevents one section of code from inadvertently changing the value of a variable used by a different section of code. This also reduces the chance of naming conflicts.

Related to the concept of scope is lifetime. Lifetime indicates how long the variable will be available. A global variable is “brought to life” when the program runs and ends when the program ends. Local variables last as long as their section of the program runs.

The data type of a variable must be considered when performing operations on variables because operators behave differently when used with different data types. For example, if a program stores the value 5 in two variables defined as integers and the + operator is used to add the variables, the program sums them and gives the value 10. However, if the values are stored as strings and then added, the result is the value 55. This process of joining together is called “concatenation.” Careful attention to using the appropriate data types can eliminate errors.

VARIABLES IN PRACTICE

A computer program may need to calculate a student's final grade based on the score the student receives on two exams. First, the variables that will hold the values for the student's name must be declared. In the pseudocode below, the variables are declared as data type string, as they will hold text information.

Next, the variables that will store the student's exam scores and final grade are declared. These variables are declared as integers, as they will hold numeric information. Failure to define the data type of these variables correctly could result in errors in calculating the student's final score.

Next, functions are used to retrieve the first and last names of the first student in the student database. These functions return textual information, and so the variables used to reference that information must be defined as strings.

This section of code uses a function to retrieve the first student's exam grades.

The student's final grade is then calculated using the addition and division operators. The calculation works correctly because both variables have been declared as integers. If the variables had been declared as strings, an error would have occurred.

Additional code can now use the values stored in these variables to display a message, create a report, or store the student's final score in a database.

THE IMPORTANCE OF VARIABLES AND VALUES

Computer programs require access to a wide range of data of varying types. In fact, the ability to access, store, and manipulate large amounts of different types of data quickly and efficiently is one of the reasons computer programs are now used in almost every facet of modern life, from controlling railroad crossing gates to landing probes on the surface of Mars. Variables and values make it possible for other constructs and statements used in computer programming to function.

—Maura Valentino, MSLIS

Friedman, Daniel P., and Mitchell Wand. Essentials of Programming Languages. 3rd ed., MIT P, 2008.

Haverbeke, Marijn. Eloquent JavaScript: A Modern Introduction to Programming. 2nd ed., No Starch Press, 2014.

MacLennan, Bruce J. Principles of Programming Languages: Design, Evaluation, and Implementation. 3rd ed., Oxford UP, 1999.

Scott, Michael L. Programming Language Pragmatics. 4th ed., Morgan Kaufmann Publishers, 2016.

Schneider, David I. An Introduction to Programming using Visual Basic. 10th ed., Pearson, 2016.

Van Roy, Peter, and Seif Haridi. Concepts, Techniques, and Models of Computer Programming. MIT P, 2004.