Encyclopedia  |   World Factbook  |   World Flags  |   Reference Tables  |   List of Lists     
   Academic Disciplines  |   Historical Timeline  |   Themed Timelines  |   Biographies  |   How-Tos     
Your Ad Here
Sponsor by The Tattoo Collection


Variable
Main Page | See live article | Alphabetical index

Variable

In computer science and mathematics, a variable denotes a quantity or symbolic representation (one which is often unknown). The opposite of a variable is a constant; for example pi is a constant.

Table of contents
1 General overview
2 Why are variables useful?
3 Computer programming
4 External link

General overview

Variables are used in open sentences. For instance, in the formula: x + 1 = 5, x is a variable which represents an "unknown" number. In mathematics, variables are usually represented by letters of the Roman alphabet, but are also represented by letters of other alphabets; as well as various other symbols. In computer programming, variables are usually represented by either single letters or alphanumeric stringss.

Why are variables useful?

Variables are useful in mathematics and computer programming because they allow instructions to be specified in a general way. If one were forced to use actual values, then the instructions would only apply in a more narrow, and specific, set of situations. For example: specify a mathematical definition for finding the square of ANY number: square(x) = x * x.

Now, all we need to do to find the square of a number is replace x with any number we want.

etc...

In the above example, the variable x is a "placeholder" for ANY number. One important thing we are assuming is that the value of each occurrence of x is the same -- that x does not get a new value between the first x and the second x. In computer programming languages without referential transparency, such changes can occur.

Computer programming

In programming languages, a variable can be thought of as a place to store a value in computer memory. Variables are convinent ways to mimic mathematnics.

Typically, the name of a variable is bound to a particular address of some bytes on the memory, and any operations on the variable would manipulate that block. This is called name binding. If the space is way too large or its size is unknown beforehand, the use of referencing is more common, in which a value is not directly stored in the variable but a location information for it is.

Importation questions about variables are twofold: its life-time and scope. For space efficiency, a memory space needed for a variable is allocated when first used and freed if no longer needed. The scope helps determine the life-time of variables. Usually, a variable is set to reside in some scope in program code, and entrance and leave of the scope coincides with the begining and ending of a varible life, respectively. Put in conceptual terms, a variable is visible in its scope, and computers could assume the varible is needed only when it is visible. In this way, however, unused variables might be given a space, which is going to be never used. Because of this, a compiler often warns programmers when a variable is declared but not used at all.

While a variable stores simple data like integers and literal strings, some languages allow a variable to store datatype as well. They enable parametric polymorphic functions to be written. They operate like variables, in that they can represent any type. For example, with the function length -- to determine the length of a list, it is only necessary to know the amount of elements in the list -- the type of the elements does not count, so the type signature can be represented with a type variable and thus is parametric polymorphic.

See name for naming rules and convention of variable names.

External link

Example of the use of variables (persons A and B) in a law