Learn more about Variable

(Redirected from Variables)
Jump to: navigation, search

In computer science and mathematics, a variable (sometimes called a pronumeral) is a symbol denoting a quantity or symbolic representation. In mathematics, a variable often represents an unknown quantity that has the potential to change; in computer science, it represents a place where a quantity can be stored. Variables are often contrasted with constants, which are known and unchanging.

The term has a similar meaning in the physical sciences and engineering: a variable is a quantity whose value may vary over the course of an experiment (including simulations), across samples, during the operation of a system. Variables are generally distinct from parameters, although what is a variable in one context may be a parameter in another. For more on this distinction, see the article on "parameter".

In applied statistics, a variable is a measurable factor, characteristic, or attribute of an individual or a system – in other words, something that might be expected to vary over time or between individuals.

In mathematical statistics, 'variable' has a technical meaning - random variables are defined in the mathematical context of measure theory as measurable functions from a probability space to a measurable space.


[edit] 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. Variables are often represented by letters of the Roman alphabet, but are also represented by letters of other alphabets, such as the Greek alphabet, as well as various other symbols.

In this sense, variables are used as a "fill the blank" within many fields - mathematics, linguistics, and so on.

[edit] In applied statistics

In statistics, variables refer to measurable attributes, as these typically vary over time or between individuals. Variables can be continuous (taking values from a continuum) or discrete (taking values from a defined set). Temperature is a continuous variable, while 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.

For example, in an experiment to test whether 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 and is therefore the dependent variable. The collection of results from an experiment, or information to be used to draw conclusions, is known as data. It is often important to consider which variables to allow for, or to directly control or eliminate, in the design of experiments.

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 general, if strongly confounding variables exist that can substantially affect the result, then this makes it more difficult to interpret the results. For example, a study into the incidence of cancer with age will also have to take into account variables such as income (poor people may have less healthy lives), location (some cancers vary depending on diet and sunlight), stress and lifestyle issues (cancer may be related to these more than age), and so on. Failure to at least consider these factors can lead to grossly inaccurate deductions. For this reason controlling unwanted variables is important in research.

Read also extraneous variables, confounding variable and intervening variable.

[edit] In mathematics

Variables are useful in mathematics 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 number twice that of ANY 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

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.

Note that in computer programming languages without referential transparency, changes such as this can occur. Variables in computer programming are also useful for this reason. The term variable, as used by programmers, is much more vague than the term as used by mathematicians. Variables allow the programmer to specify relationships between data.

[edit] In computer programming

In computer programming, variables are usually represented by alphanumeric strings. A variable can be thought of as a place to store a value in computer memory.

More precisely, a variable binds (associates) a name (sometimes called an identifier) with the location; a value in turn is stored as a data object in that location so that the object can be accessed later via the variable, much like referring to someone by name. Variables in computer programming are analogous to variables in mathematics. Put in another way, an object could exist without being bound to a variable, but without such a referent, it would be inaccessible from code.This is more analogous to calling out a person whose name is not known, and expecting a reply from him or her.

[edit] Variable names

See also: Identifier and Namespace (computer science)

Variables are denoted by identifiers.

In some programming languages, specific characters are prepended 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 prefixes $, @, %, and & indicate scalar, array, hash, and subroutine variables, respectively.

Following is an example of variable declarations and assignments in C++ and C#:

int a = 10;
string b = "hello";

[edit] Scope and extent

The scope of a variable describes where in a program's text a variable may be used, while extent (or lifetime) describes when in a program's execution a variable has a value. A variable's scope affects its extent.

Scope is a lexical aspect of a variable. Most languages define a specific scope for each variable, 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 life-time and exit from that scope typically ends its life-time. 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. It is erroneous to refer to a variable where it is out of scope. Lexical analysis of a program can determine whether variables are used out of scope. In compiled languages, such analysis can be performed statically, at compile-time.

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 a 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 life-time 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 type, meaning that only values of a given class (or set of classes) can be stored in it. In dynamically-typed languages such as Python, it is values and 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 type of the value as opposed to which of 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. Such 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 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.

[edit] Memory allocation

The specifics of variable allocation and the representation of their values vary widely, both among programming languages and among implementations of any given language. Many language implementations allocate space for local variables, whose extent lasts for a single function call, on the call stack, 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 value have large or unknown sizes when the code is compiled. Such variables reference the location of (i.e. the pointer to) 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 re-use 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 "reaps" objects when extant variables can no longer refer to them. In non-garbage-collected languages, such as C, the program (and thus the programmer) must explicitly allocate memory and 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 of 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 the case where only a portion of the memory reachable from a variable needs to be reclaimed.

[edit] Mutable vs. immutable

See also: Immutable object

Variables can be either mutable or immutable. Mutable variables are said to have both an l-value and an r-value, but immutable have only an r-value. In functional programming, all variables are immutable. Because immutable variables are semantically the same as named constants or constant functions, the term "variable" generally usually indicates a mutable variable.

In C++, member variables are immutable from "const" member functions; the keyword "mutable" on a member variable modifies the default behavior and allows const members functions to modify that variable.

[edit] Constants

A constant variable is similar to an immutable variable, but while the value referenced by an immutable variable cannot change during program execution, with a constant it is the location referenced by the constant that cannot change. Since constant variables are often bound to literal values, which are themselves immutable, constant variables are often themselves immutable (e.g. const int HoursPerDay=24). Although a constant value is specified only once, the constant variable can be referenced multiple times in a program. Using a constant instead of specifying a value multiple times in the program can simplify code maintenance, not only to simplify changing its value but also to supply a meaningful name for it and to consolidate such constant assignments 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 to static constant a fixed value, which will be known before the program starts. 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 Java allow assigning to a dynamic constant an expression, possibly involving non-constant operands. The value of such constants may rely on variables defined while a computer program is running. Thus, unlike static constants, the values of dynamic constants cannot be determined at compile time. E.g.: final int a = b + 20;.

Although such dynamic constants have fixed values if they are primitive types (e.g. int), their values may change if they contain a reference to an object, as shown in the following example:

final StringBuffer sampleDynamicConstant = new StringBuffer ("InitialValueOfDynamicConstant");

The above code produces the following output:


[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.

For example, the following Perl code:

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

produces the output:

   Nancy said Hello World to the crowd of people.

[edit] See also

da:Variabel de:Variable et:Muutuja es:Variable (programación) eo:Variablo fa:متغیر fr:Variable he:משתנה hu:Változó it:Variabile nl:Variabele ja:変数 pl:Zmienna (informatyka) ro:Variabilă ru:Переменная sk:Premenná sl:spremenljivka sv:Variabel vi:Biến số zh:變數


Personal tools
what is world wizzy?
  • World Wizzy is a static snapshot taken of Wikipedia in early 2007. It cannot be edited and is online for historic & educational purposes only.