A variable is something which is subject to change; the term is most commonly used in computer science and mathematics, where it denotes a quantity or symbolic representation (one which is often unknown).
Table of contents |
2 Why Are Variables Useful? 3 Computer Science |
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.
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 Science
In most programming languages, there are 2 types of variables: global and local variables. Global variables exist throughout a program, whereas local variables only exist within a given statement block or function. In some languages, variables are defined via an explicit declaration, in others they are declared implicitly by their first use.