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.
square(x) = x * x = y
square(1) = 1 * 1 = 1
square(2) = 2 * 2 = 4
square(3) = 3 * 3 = 9
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 computerprogramming languages without referential transparency, such changes can occur.
When one begins using a given variable, the language interpreter or compiler typically sets aside a space in memory to store the value given to that variable. Later, when the variable is no longer in use, this space can be reclaimed.
Variables are referred to by a name which is used to refer to its contents. A variable's name can contain text and numbers, but there are restrictions to avoid problems in lexical analysis. For example, a variable in C might be called height or numberOfCats or cow_name. In some languages or programming practices, the name of a variable can tell you what kind of values you might find in it. For instance, in Fortran, the first letter in a variable's name indicates whether by default it is created as an integer or floating point variable. In BASIC, the suffix $ on a variable name indicates that its value is a string. Perl uses the prefixes $, @, %, and & to indicate scalar, array, hash, and subroutine variables. In Common Lisp, variables' names are not strings, but symbols -- a special data type. See also identifier.
In most languages, variables can have different '\'scopes. The scope of a variable is the portion of the program code for which the variable's name has meaning. For instance, a variable with lexical scope is meaningful only within a certain block of statements or subroutine. A global variable, or one with indefinite scope'', may be referred to anywhere in the program. When a variable has gone out of scope, it is erroneous or meaningless to refer to it.
Likewise, the bindingss of variables to values can have different extent. The extent of a binding is the length of time -- part of the course of the program's execution -- during which the variable continues to refer to the same value. A variable can be unbound, meaning that it is in scope but has never been given a value, or its value has been destroyed; in many languages, it is an error to try to use the value of an unbound variable, or may yield unpredictable results.
In statically-typed languages such as Java or ML, a variable also has type, meaning that only values of a given sort can be stored in it. In dynamically-typed languages such as Python or Lisp, it is values and not variables which carry type. See type system.
The arguments or formal parameters of functions are also referred to as variables. For instance, in these equivalent functions in Python and Lisp
def addtwo(x):
return x + 2
(defun addtwo (x) (+ x 2))
the variable named x is an argument. It is given a value when the function is called. In most languages, function arguments have local scope; this specific variable named x can only be referred to within the addtwo function, though of course other functions can also have variables called x.
When you are done with a variable, the space that its value takes up in memory can be reclaimed. Depending on language, this can take place in a number of ways. In garbage-collected languages, such as Lisp and Java, when the system can prove that a value can no longer be referred to, the memory that it takes up is marked to be recycled. Eventually, a subprocess called the garbage collector reclaims the memory -- it is said to be "gc'd". In other languages, such as C and C++, certain kinds of variables must be explicitly allocated and freed by the programmer. See memory management.