variable
In algebra, a symbol (usually a letter) standing in for an unknown numerical value in an equation. Commonly used variables include x and y (real-number unknowns), z (complex-number unknowns), t (time), r (radius), and s (arc length). Variables should be distinguished from coefficients, fixed values that multiply powers of variables in polynomials and algebraic equations. In the quadratic equation ax2 + bx + c = 0, x is the variable, and a, b, and c are coefficients whose values must be specified to solve the equation. In translating word problems into algebraic equations, quantities to be determined can be represented by variables.
In programming, a structure that holds data and is uniquely named by the programmer. It holds the data assigned to it until a new value is assigned or the program is finished.
Control Values
Variables are widely used to hold control values that keep track of something. For example, the C statement FOR (X=0; X<5; X++) performs the instructions following the statement within open and closed curly braces ({ and }) five times, and X is keeping track of that number of iterations. X is a variable set to zero (x=0), incremented by 1 (x++) and compared to 5 (x<5). The reason it is less than 5 (<5) is because we started with 0.
The Equals Sign
Variables are usually assigned with an equal sign. Numbers are unquoted; for example: COUNTER = 1 places the digit 1 in the variable COUNTER. Character data (text) requires quotes; for example: PRODUCT = "abc1234". In some languages, the type of data must be declared before it is assigned; for example, in C/C++, the statement INT COUNTER creates a variable that holds only whole numbers (integers).
Local and Global
A local variable is one that is referenced only within the subprogram, function or procedure it was defined in. A global variable can be used by the entire program. See undefined variable and local variable.
| (programming) | variable - (Sometimes "var" /veir/ or /var/) A named memory
location in which a program can store intermediate results and
from which it can read it them. Each programming language
has different rules about how variables can be named, typed,
and used. Typically, a value is "assigned" to a variable in
an assignment statement. The value is obtained by
evaluating an expression and then stored in the variable. For
example, the assignment
x = y + 1
means "add one to y and store the result in x". This may look
like a mathematical equation but the mathematical equality is
only true in the program until the value of x or y changes.
Furthermore, statements like
x = x + 1
are common. This means "add one to x", which only makes sense
as a state changing operation, not as a mathematical equality.
The simplest form of variable corresponds to a single-word
of memory or a CPU register and an assignment to a
load or store machine code operation.
A variable is usually defined to have a type, which never
changes, and which defines the set of values the variable can
hold. A type may specify a single ("atomic") value or a
collection ("aggregate") of values of the same or different
types. A common aggregate type is the array - a set of
values, one of which can be selected by supplying a numerical
index.
Languages may be untyped, weakly typed, strongly typed,
or some combination. Object-oriented programming languages
extend this to object types or classes.
A variable's scope is the region of the program source
within which it represents a certain thing. Scoping rules are
also highly language dependent but most serious languages
support both local variables and global variables.
Subroutine and function formal arguments are special
variables which are set automatically by the language runtime
on entry to the subroutine.
In a functional programming language, a variable's value
never changes and change of state is handled as recursion over
lists of values. | |