Monday, March 9, 2009

☻variables☻

Variable
From Wikipedia, the free encyclopedia
Jump to: navigation, search
Image:Split-arrows.svg
It has been suggested that this article be split into multiple articles accessible from a disambiguation page. (Discuss)


A variable (pronounced /ˈvɛərɪəbl/) is a symbol that stands for a value that may vary; the term usually occurs in opposition to constant, which is a symbol for a non-varying value, i.e. completely fixed or fixed in the context of use.

Variables need not be numeric. In a spreadsheet, the variable lastname might stand for a customer's last name. The concepts of constants and variables are basic to all modern mathematics, science, engineering, and computer programming.
Contents
[hide]

* 1 What it means for a variable to vary
o 1.1 Actual change
+ 1.1.1 Example
o 1.2 Dependence on other variables
+ 1.2.1 Examples
* 2 Notation for variables
* 3 History
* 4 In mathematics
o 4.1 General introduction
o 4.2 Name conventions
o 4.3 Applied statistics
* 5 In computer programming
o 5.1 Naming conventions
o 5.2 Variables in source code
o 5.3 Variables in spreadsheets
o 5.4 Scope and extent
o 5.5 Typed and untyped variables
o 5.6 Parameters
o 5.7 Memory allocation
o 5.8 Constants
o 5.9 Variable interpolation
* 6 Murphy's Law
* 7 See also
* 8 References

[edit] What it means for a variable to vary

[edit] Actual change

In some contexts of use, the varying of variables is literal: their values actually change.

[edit] Example

For instance, in imperative programming, variables are set and usually modified by the running program through explicit assignment. In a C program we can say:

int r = 5;
while (r*r < 100)
{
r = r + 1;
}

[edit] Dependence on other variables

Mathematics, the sciences, and non-imperative forms of programming such as pure functional programming or logical programming take a more abstract view: varying doesn't mean change, but rather, dependence on the values of other variables in the expression in which the variable occurs, or dependence of the value of the expression on that of the variable.

[edit] Examples

In the expression from physics E = m c2, E and m are variables, while c is a constant, the velocity of light in vacuo. This famous equation describes how the value E and m are related, but that c is fixed.

In the ideal gas law, PV = nRT describes the relationship between volume V, pressure P and temperature T; here, n is a constant, but a chemist will try to juggle one or more of the variables to fit the others, knowing full they can't beat the law.

In mathematics, in the theory of polynomials, a second-order polynomial is generally denoted as a x2 + b x + c, where a, b and c are called constant terms while x is called a variable, as what is usually being studied is how the value of the function varies with x, for given values of a, b and c.

[edit] Notation for variables

In mathematics, single-symbol names for variables are the norm, with x, y, z, and t being most common; constants are usually denoted as a, b, c. In written mathematics, variables and constants are usually set in an italic typeface.

Specific branches and applications of mathematics usually have specific naming conventions for variables. Variables with similar roles or meanings are often assigned consecutive letters. For example, the three axes in 3D coordinate space are conventionally called x, y, and z, while random variables in statistics are usually named X, Y, Z. In physics, the names of variables are largely determined by the physical quantity they describe, but various naming conventions exist.

A convention sometimes followed in statistics is to use X, Y, Z for the names of random variables, with these being replaced by x, y, z for observations or sample outcomes of those random variabes. Another convention sometimes used in statistics is to denote population values of particular statistics by lower (or upper) case greek letters, with sample-based estimates of those quantities being denoted by the corresponding lower (or upper) case letters from the ordinary alphabet.

In computer programming and other computer applications, variables and constants commonly take multiple-character names, e.g. COST or total. Single-character names are most commonly used for auxiliary variables; typically, i, j, k for integer variables, and x, y, z for real-valued ones.

[edit] History

Much of the basic theory for which we use variables today, such as school geometry and algebra, was developed thousands of years ago, but the use of symbolic formulae and variables is only several hundreds of years old. Arabic algebra texts such as the Al-Jabr still described mathematics in full text rather than symbolic formulae, but denoted the variable quantity in Arabic word šay' شَيْء = “thing”;. This was taken into Old Spanish with the pronunciation “šei”, which was written xei, and was soon habitually abbreviated to x; and this is still the customary variable name in many fields today. It started the habit of using letters to represent variables in algebra. Beyond mathematics, “X” has come to represent a generic placeholder variable whose value is unknown or secret, as in project X or mister X.

[edit] In mathematics

[edit] General introduction

Variables are used in open sentences. For instance, in the formula x + 1 = 5, x is a variable which represents an "unknown" number. Variables are often represented by letters of the Roman alphabet, or those of other alphabets, such as Greek, and use other special symbols.

In mathematics, variables are essential because they let quantitative relationships to be stated in a general way. If we were forced to use actual values, then the relationships would only apply in a more narrow set of situations.For example:

State a mathematical definition for finding the number twice that of ANY other finite number:

double(x) = x + x.

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

* double(1) = 1 + 1 = 2
* double(3) = 3 + 3 = 6
* double(55) = 55 + 55 = 110
* etc.

So in this example, the variable x is a "placeholder" for any number— that is to say, a variable. One important thing we assume is that the value of x does not change, even though we don't know what x is. But in some algorithms, obviously, will change x, and there are various ways to then denote if we mean its old or new value— again, generally not knowing either, but perhaps (for example) that one is less than the other.

[edit] Name conventions

Mathematics has many conventions. Below are some of the more common. Many of the symbols have other conventional uses, but they may actually represent a constant or a specific function rather than a variable.

* ai is often used to denote a term of a sequence.
* a, b, c, and d (sometimes extended to e and f) usually play similar roles or are made to represent parallel notions in a mathematical context. They often represent constants.
o The coefficients in an equation, for example the general expression of a polynomial or a Diophantine equation are often a, b, c, d, e, and f.
* f and g (sometimes h) commonly denote functions.
* i, j, and k are often used as subscripts or index variables.
* l and w are often used to represent the length and width of a figure.
* m and n usually denote integers and usually play similar roles or are made to represent parallel notions in a mathematical context.
o n typically denotes a count of objects, or, in statistics, the number of individuals.
* p and q often represent prime numbers, or relatively prime numbers.
* p, q, and r usually play similar roles or are made to represent parallel notions in a mathematical context.
* r often denotes a remainder or modulus.
* r, s, and t usually play similar roles or are made to represent parallel notions in a mathematical context.
* u and v usually play similar roles or are made to represent parallel notions in a mathematical context, such as denoting a vertex (graph theory).
* w, x, y, and z usually play similar roles or are made to represent parallel notions in a mathematical context, such as representing unknowns in an equation.
* x, y and z correspond to the three Cartesian axes. In many two-dimensional cases, y will be expressed in terms of x; if a third dimension is added, z is expressed in terms of x and y.
* z is a common variable for a complex number.
* α, β, γ, θ and φ commonly denote angle measures.
* ε usually represents an arbitrarily small positive number.
* λ is used for eigenvalues.
* σ often denotes a sum, or the standard deviation in a statistical context.

[edit] Applied statistics

In statistics, variables refer to measurable attributes, as these typically vary over time or between individuals. Variables can be discrete (taking values from a finite or countable set), continuous (having a continuous distribution function), or neither. Temperature is a continuous variable, while the number of legs of an animal is a discrete variable. This concept of a variable is widely used in the natural, medical and social sciences.

In causal models, a distinction is made between "independent variables" and "dependent variables", the latter being expected to vary in value in response to changes in the former. In other words, an independent variable is presumed to potentially affect a dependent one. In experiments, independent variables include factors that can be altered or chosen by the researcher independent of other factors.

So, in an experiment to test if the boiling point of water changes with altitude, the altitude is under direct control and is the independent variable, and the boiling point is presumed to depend upon it, so being the dependent variable. The results of an experiment, or information to be used to draw conclusions, are known as data. It is often important to consider which variables to allow, or directly control or eliminate, in the design of experiments.

There are also quasi-independent variables, which are used by researchers to group things without affecting the variable itself. For example, to separate people into groups by their sex does not change whether they are male of female. Or a researcher may separate people, arbitrarily, on the amount of coffee they had drunk before beginning an experiment. The researcher cannot change the past, but can use it to split people into groups.

While independent variables can refer to quantities and qualities that are under experimental control, they can also include extraneous factors that influence results in a confusing or undesired manner. In statistics the techique to work this out is called correlation.

If strongly confounding variables exist that can substantially change the result, it makes it harder to interpret. For example, a study on cancer against age will also have to take into account variables such as income, location, stress and lifestyle. Without considering these, the results could be grossly inaccurate deductions. Because of this, controlling unwanted variables is important in research.
See also: extraneous variables, intervening variable, and level of measurement

[edit] In computer programming

In computer programming, a variable is a keyword or phrase (identifier) that is linked to a value stored in the system's memory or an expression that can be evaluated. For instance, a variable might be called 'total_count' and contain a number.

In imperative programming values can generally be accessed (dereferenced) or changed (assigned) at any time. In pure functional and logic languages, which have referential transparency, variables are bound to expressions and keep a single value during their whole scope.

Some programming languages, such as C++, require strict adherence to data type: each variable is created with a certain type, such as integer or string, and can only store data that conforms to it. Other languages, such as JavaScript, allow variables to be treated as whatever data type is most convenient at the time. The former approach typically allows faster programming and faster execution, while the latter initially takes more time but helps prevent programming mistakes, which may eventually save time.

[edit] Naming conventions
Main article: Naming conventions (programming)
See also: Identifier and Namespace (computer science)

In some programming languages, specific characters (known as sigils) are prefixed or appended to variable identifiers to indicate the variable's type. For example:

* In BASIC, the suffix $ on a variable name indicates that its value is a string;
* In Perl, the sigils $, @, %, and & indicate scalar, array, hash, and subroutine variables, respectively.
* In spreadsheets variables can refer to cells (e.g. $A$2), named ranges, or values in associated source code or functions.

[edit] Variables in source code

In computer source code, a variable name is one way to bind a variable to a memory location; the corresponding value is stored as a data object in that location so that the object can be accessed and manipulated later via the variable's name.

[edit] Variables in spreadsheets

In a spreadsheet, a cell may contain a formula with references to other cells. Such a cell reference is a kind of variable; its value is the value of the referenced cell (see also: reference (computer science)).

[edit] Scope and extent
See also: Free variables and bound variables

The scope of a variable describes where in a program's text, the variable may be used, while the extent (or lifetime) describes when in a program's execution a variable has a value. The scope of a variable is actually a property of the name of the variable, and the extent is a property of the variable itself.

A variable name's scope affects its extent.

Scope is a lexical aspect of a variable. Most languages define a specific scope for each variable (as well as any other named entity), which may differ within a given program. The scope of a variable is the portion of the program code for which the variable's name has meaning and for which the variable is said to be "visible". Entrance into that scope typically begins a variable's lifetime and exit from that scope typically ends its lifetime. 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.

Extent, on the other hand, is a runtime (dynamic) aspect of a variable. Each binding of a variable to a value can have its own extent at runtime. The extent of the binding is the portion of the program's execution time during which the variable continues to refer to the same value or memory location. A running program may enter and leave a given extent many times, as in the case of a closure.

In portions of code, a variable in scope may never have been given a value, or its value may have been destroyed. Such variables are described as "out of extent" or "unbound". In many languages, it is an error to try to use the value of a variable when it is out of extent. In other languages, doing so may yield unpredictable results. Such a variable may, however, be assigned a new value, which gives it a new extent. By contrast, it is permissible for a variable binding to extend beyond its scope, as occurs in Lisp closures and C static variables. When execution passes back into the variable's scope, the variable may once again be used.

For space efficiency, a memory space needed for a variable may be allocated only when the variable is first used and freed when it is no longer needed. A variable is only needed when it is in scope, but beginning each variable's lifetime when it enters scope may give space to unused variables. To avoid wasting such space, compilers often warn programmers if a variable is declared but not used.

It is considered good programming practice to make the scope of variables as narrow as feasible so that different parts of a program do not accidentally interact with each other by modifying each other's variables. Doing so also prevents action at a distance. Common techniques for doing so are to have different sections of a program use different namespaces, or to make individual variables "private" through either dynamic variable scoping or lexical variable scoping.

Many programming languages employ a reserved value (often named null or nil) to indicate an invalid or uninitialized variable.

[edit] Typed and untyped variables

In statically-typed languages such as Java or ML, a variable also has a type, meaning that only values of a given class (or set of classes) can be stored in it. A variable of a primitive type holds a value of that exact primitive type. A variable of a class type can hold a null reference or a reference to an object whose type is that class type or any subclass of that class type. A variable of an interface type can hold a null reference or a reference to an instance of any class that implements the interface. A variable of an array type can hold a null reference or a reference to an array.

In dynamically-typed languages such as Python, it is values, not variables, which carry type. In Common Lisp, both situations exist simultaneously: a variable is given a type (if undeclared, it is assumed to be T, the universal supertype) which exists at compile time. Values also have types, which can be checked and queried at runtime. See type system.

Typing of variables also allows polymorphisms to be resolved at compile time. However, this is different from the polymorphism used in object-oriented function calls (referred to as virtual functions in C++) which resolves the call based on the value type as opposed to the supertypes the variable is allowed to have.

Variables often store simple data-like integers and literal strings, but some programming languages allow a variable to store values of other datatypes as well. Such languages may also enable functions to be parametric polymorphic. These functions operate like variables to represent data of multiple types. For example, a function named length may determine the length of a list. Such a length function may be parametric polymorphic by including a type variable in its type signature, since the amount of elements in the list is independent of the elements' types.

[edit] Parameters

The formal parameters of functions are also referred to as variables. For instance, in this Python code segment,

def addtwo(x):
return x + 2

addtwo(5) # yields 7

the variable named x is a parameter because it is given a value when the function is called. The integer 5 is the argument which gives x its value. In most languages, function parameters have local scope[citation needed]. 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).

[edit] Memory allocation

The specifics of variable allocation and the representation of their values vary widely, both among programming languages and among implementations of a given language. Many language implementations allocate space for local variables, whose extent lasts for a single function call on the call stack, and whose memory is automatically reclaimed when the function returns. (More generally, in name binding, the name of a variable is bound to the address of some particular block (contiguous sequence) of bytes in memory, and operations on the variable manipulate that block. Referencing is more common for variables whose values have large or unknown sizes when the code is compiled. Such variables reference the location of the value instead of the storing value itself, which is allocated from a pool of memory called the heap.

Bound variables have values. A value, however, is an abstraction, an idea; in implementation, a value is represented by some data object, which is stored somewhere in computer memory. The program, or the runtime environment, must set aside memory for each data object and, since memory is finite, ensure that this memory is yielded for reuse when the object is no longer needed to represent some variable's value.

Objects allocated from the heap must be reclaimed specially when the objects are no longer needed. In a garbage-collected language (such as C#, Java, and Lisp), the runtime environment automatically reclaims objects when extant variables can no longer refer to them. In non-garbage-collected languages, such as C, the program (and the programmer) must explicitly allocate memory, and then later free it, to reclaim its memory. Failure to do so leads to memory leaks, in which the heap is depleted as the program runs, risking eventual failure from exhausting available memory.

When a variable refers to a data structure created dynamically, some of its components may be only indirectly accessed through the variable. In such circumstances, garbage collectors (or analogous program features in languages that lack garbage collectors) must deal with a case where only a portion of the memory reachable from the variable needs to be reclaimed.

[edit] Constants

A constant is something whose value does not change in the course of the problem under investigation. As noted above, which symbols represent variables and which represent constants is to a large extent arbitrary, and must be specified in each particular problem, though conventions about which letters stand for constants and which letters stand for variables is often assumed. (For example, most beginning students of calculus will say that the derivative of xn is nxn - 1, but this is only true under the assumption that x is a variable and n is a constant.) In a spreadsheet, in one problem it may be useful to hold DATE constant and vary COST; in another problem it may be useful to hold COST constant and vary DATE. In the first case, we might be looking at the cost of a particular item on a particular date from various vendors. In the second case we might be looking for the date on which we could buy an item for a particular price.

In many machine assembly languages or instruction set specifications, constant values are termed immediate values because they are available immediately (often embedded as part of the instruction stream) without needing to load a value from the data cache[1].

Although a constant value is specified only once, the constant can be referenced multiple times in a program. Using a constant instead of specifying a value multiple times in the program can not only simplify code maintenance, but it can also supply a meaningful name for it and consolidate such constant bindings to a standard code location (for example, at the beginning).

Programming languages provide one of two kinds of constant variables:

Static constant or Manifest constant

Languages such as Visual Basic allow assigning a fixed value to static constant which will be known at compile time. Such a constant has the same value each time its program runs. Changing the value is accomplished by changing (and possibly recompiling) the code. E.g.: CONST a = 60.
Dynamic constant
Languages such as C++ and Java allow initializing a dynamic constant with a value that is computed at runtime. Unlike static constants, the values of dynamic constants cannot be determined until run time. For example: final int a = b + 20;.

For variables which are references, do not confuse constant references with immutable objects. For example, when a non-constant reference references an immutable object, that reference can be changed to reference a different object, but the object it originally pointed to cannot be changed (i.e. other references that reference it still see the same information).

Conversely, a constant reference may reference a mutable object. In this case, the reference will always reference the same object (the reference cannot be changed); however, the object which is referenced can still be changed (and other references which also reference that object will see the change), as shown in the following example:

final StringBuffer sampleDynamicConstant = new StringBuffer ("InitialValueOfDynamicConstant");
sampleDynamicConstant.append("_AppendedText");
System.out.println(sampleDynamicConstant);

The above code produces the following output:

InitialValueOfDynamicConstant_AppendedText

In languages where a variable can be an object (i.e. C++), such a variable being constant is equivalent to the immutability of that object.

A popular naming convention for symbolic constants is to give them a name wherein all the letters are capitalized, sometime separated by underscores; for example: SOME_CONSTANT

[edit] Variable interpolation

Variable interpolation (also variable substitution, variable expansion) is the process of evaluating an expression or string literal containing one or more variables, yielding a result in which the variables are replaced with their corresponding values in memory. It is a specialized instance of concatenation.

Languages that support variable interpolation include Perl, PHP, Ruby, and most Unix shells. In these languages, variable interpolation only occurs when the string literal is double-quoted, but not when it is single-quoted. The variables are recognized because variables start with a sigil (typically "$") in these languages.

For example, the following Perl code:

$name = "Nancy";
print "$name said Hello World to the crowd of people.";

produces the output:

Nancy said Hello World to the crowd of people.

Ruby uses the "#" symbol for interpolation, and lets you interpolate any expression, not just variables.

[edit] Murphy's Law

Murphy's Law predicts that constants aren't and variables don't.

No comments: