Variable

In computer science and mathematics, a variable is a symbol denoting a quantity or symbolic representation. In mathematics, a variable often represents an unknown quantity; in computer science, it represents a place where a quantity can be stored. Variables are often contrasted with constants, which are known and unchanging.

In other scientific fields such as biology, chemistry, and physics, the word variable is used to refer to a measurable factor, characteristic, or attribute of an individual or a system. In a scientific experiment, so called "independent variables" are factors that can be altered by the scientist. For example, temperature is a common environmental factor that can be controlled in laboratory experiments. "Dependent variables" or "response variables" are those that are measured and collected as data.

Contents

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

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.

  • 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 computer programming languages without referential transparency, such changes can occur.

Computer programming

In programming languages, a variable can be thought of as a place to store a value in computer memory.

In general, a variable binds an object to a name so that the object could be accessed later, much like a person has a name and people could refer to him by that name. This is analogous to the use of variables in the mathematics and variables in computer programming work usually in the similar manner. Put in another way, an object could exist without it being bound to a certain variable.

Typically, the name of a variable is bound to a particular address of some bytes on the memory, and any operations on the variable would manipulate that block. This is called name binding. If the space is way too large or its size is unknown beforehand, the use of referencing is more common, in which a value is not directly stored in the variable but a location information for it is.

Importation questions about variables are twofold: its life-time and scope. For space efficiency, a memory space needed for a variable is allocated when first used and freed if no longer needed. The scope helps determine the life-time of variables. Usually, a variable is set to reside in some scope in program code, and entrance and leave of the scope coincides with the beginning and ending of a variable life, respectively. Put in conceptual terms, a variable is visible in its scope, and computers could assume the variable is needed only when it is visible. In this way, however, unused variables might be given a space, which is going to be never used. Because of this, a compiler often warns programmers when a variable is declared but not used at all.

While a variable stores simple data like integers and literal strings, some languages allow a variable to store datatype as well. They enable parametric polymorphic functions to be written. They operate like variables, in that they can represent any type. For example, with the function length -- to determine the length of a list, it is only necessary to know the amount of elements in the list -- the type of the elements does not count, so the type signature can be represented with a type variable and thus is parametric polymorphic.

Variables could be either mutable or immutable. Mutable variables could be thought of ones having l-value while immutable ones having r-value. One characteristic of functional programming is that a variable is immutable. Because immutable variables are semantically the same as constants given a name or constant functions, when one talks about variables, they usually mean mutable variables.

See name for naming rules and convention of variable names.

In C++ (not in C), "mutable" is a keyword to allow a mutable member to be modified by a const member function.


In programming languages, a variable can be thought of as a place to store a value in computer memory. More precisely, a variable associates a name (sometimes called an identifier) with the location of the value; the value in turn is stored as a data object in this location. The specifics of variable allocation and the representation of values vary widely, both among languages and among implementations of any given language.

Constant

A constant is similar, but does not change its value during program execution, and is the same when running the program again. The value is specified only once but the constant can be referenced multiple times in the program. Changing the value is done by a simple change of the program. Using a constant should be distinguished from specifying the value multiple times in the program. The latter would make a change of the value cumbersome. Even if the value is used in one program location only, advantages over just putting the value there are that the name of the constant can be informative about its meaning, and the value is specified at a more convenient standard location in the program, such as at the beginning.

Variables names

Variables are denoted by identifiers.

In some 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.

The identifier article describes cases where the actual identifier itself, rather than a prefix or suffix, is interpreted by the compiler or interpreter.

See also:

Scope and extent

The scope and extent (or lifetime) of a variable describe where in the program's text it may be used, and when in the program's execution it has a value.

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. Lexical analysis of a program can determine whether variables are used out of scope.

Likewise, the bindings 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 or place. A running program may enter and leave a given extent many times, as in the case of a closure. 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 other words, scope is a lexical fact, but extent a runtime (dynamic) fact. If a variable name is out of scope, then it is an error for that name to be used in the program code. In compiled languages, this error can be detected statically at compile-time. If a variable is out of extent, its value cannot be referred to (since it doesn't have one; it is unbound) but it may be given a value, which gives it a new extent.

When a variable binding extends (in time) as the program's execution passes out of the variable's scope, this is no bug. It is a Lisp closure or a C static variable: when execution passes back into the variable's scope, the variable may be referred to again. But when a variable's extent ends, it becomes unbound -- if it is still in scope, referring to it is an error (or, in C, gets you a nice arbitrary value).

It is considered good programming practice to make variables as narrow a scope as feasible so that different parts of a program do not accidentally interact with each other by modifying each other's variables. This also prevents action at distance. Common techniques for doing so are to have different sections of your program use different namespaces, or else 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.

Memory allocation

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.

The handling of memory for variables is highly dependent on the programming language environment. Many language implementations handle the simplest cases easily by distinguishing those variables whose extent lasts no longer than a single function call. Space for these local variables are allocated on the execution stack, where their memory is automatically reclaimed when the function returns.

Space for other objects must be allocated on the heap, or pool of unused memory. These must be reclaimed specially when the objects are no longer needed. In a garbage-collected (gc) language such as Java or Lisp, the runtime environment automatically "reaps" objects when it can be proven that no extant variable refers to them. In a non-gc language such as C, it is up to the program (and thus the programmer) to explicitly allocate memory; and in turn to state when memory can be reclaimed, by explicitly freeing it. Failure to do so leads to memory leaks, in which the heap is depleted over the program's run. If the program runs long enough, it will exhaust available memory and fail.

Memory allocation goes beyond single variables. A variable may refer to a data structure created dynamically, where many structure components are not directly named by variables, but are reachable from a variable by traversing the structure. For this reason, garbage collectors (and programs in languages which lack them) must deal with the case where a portion of the memory reachable from a variable needs to be reclaimed.

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

Typing of variables also allows polymorphisms to be resolved at compile time.

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.

External link

Template:Selfrefde:Variable eo:Variablo es:Variable (programación) et:Muutuja fr:Variable ja:変数 nl:Variabele pl:Zmienna (informatyka) ru:Переменная sk:Premenná sl:spremenljivka sv:Variabel zh:變數 hu:Változó vi:Biến số

Navigation

  • Art and Cultures
    • Art (https://academickids.com/encyclopedia/index.php/Art)
    • Architecture (https://academickids.com/encyclopedia/index.php/Architecture)
    • Cultures (https://www.academickids.com/encyclopedia/index.php/Cultures)
    • Music (https://www.academickids.com/encyclopedia/index.php/Music)
    • Musical Instruments (http://academickids.com/encyclopedia/index.php/List_of_musical_instruments)
  • Biographies (http://www.academickids.com/encyclopedia/index.php/Biographies)
  • Clipart (http://www.academickids.com/encyclopedia/index.php/Clipart)
  • Geography (http://www.academickids.com/encyclopedia/index.php/Geography)
    • Countries of the World (http://www.academickids.com/encyclopedia/index.php/Countries)
    • Maps (http://www.academickids.com/encyclopedia/index.php/Maps)
    • Flags (http://www.academickids.com/encyclopedia/index.php/Flags)
    • Continents (http://www.academickids.com/encyclopedia/index.php/Continents)
  • History (http://www.academickids.com/encyclopedia/index.php/History)
    • Ancient Civilizations (http://www.academickids.com/encyclopedia/index.php/Ancient_Civilizations)
    • Industrial Revolution (http://www.academickids.com/encyclopedia/index.php/Industrial_Revolution)
    • Middle Ages (http://www.academickids.com/encyclopedia/index.php/Middle_Ages)
    • Prehistory (http://www.academickids.com/encyclopedia/index.php/Prehistory)
    • Renaissance (http://www.academickids.com/encyclopedia/index.php/Renaissance)
    • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
    • United States (http://www.academickids.com/encyclopedia/index.php/United_States)
    • Wars (http://www.academickids.com/encyclopedia/index.php/Wars)
    • World History (http://www.academickids.com/encyclopedia/index.php/History_of_the_world)
  • Human Body (http://www.academickids.com/encyclopedia/index.php/Human_Body)
  • Mathematics (http://www.academickids.com/encyclopedia/index.php/Mathematics)
  • Reference (http://www.academickids.com/encyclopedia/index.php/Reference)
  • Science (http://www.academickids.com/encyclopedia/index.php/Science)
    • Animals (http://www.academickids.com/encyclopedia/index.php/Animals)
    • Aviation (http://www.academickids.com/encyclopedia/index.php/Aviation)
    • Dinosaurs (http://www.academickids.com/encyclopedia/index.php/Dinosaurs)
    • Earth (http://www.academickids.com/encyclopedia/index.php/Earth)
    • Inventions (http://www.academickids.com/encyclopedia/index.php/Inventions)
    • Physical Science (http://www.academickids.com/encyclopedia/index.php/Physical_Science)
    • Plants (http://www.academickids.com/encyclopedia/index.php/Plants)
    • Scientists (http://www.academickids.com/encyclopedia/index.php/Scientists)
  • Social Studies (http://www.academickids.com/encyclopedia/index.php/Social_Studies)
    • Anthropology (http://www.academickids.com/encyclopedia/index.php/Anthropology)
    • Economics (http://www.academickids.com/encyclopedia/index.php/Economics)
    • Government (http://www.academickids.com/encyclopedia/index.php/Government)
    • Religion (http://www.academickids.com/encyclopedia/index.php/Religion)
    • Holidays (http://www.academickids.com/encyclopedia/index.php/Holidays)
  • Space and Astronomy
    • Solar System (http://www.academickids.com/encyclopedia/index.php/Solar_System)
    • Planets (http://www.academickids.com/encyclopedia/index.php/Planets)
  • Sports (http://www.academickids.com/encyclopedia/index.php/Sports)
  • Timelines (http://www.academickids.com/encyclopedia/index.php/Timelines)
  • Weather (http://www.academickids.com/encyclopedia/index.php/Weather)
  • US States (http://www.academickids.com/encyclopedia/index.php/US_States)

Information

  • Home Page (http://academickids.com/encyclopedia/index.php)
  • Contact Us (http://www.academickids.com/encyclopedia/index.php/Contactus)

  • Clip Art (http://classroomclipart.com)
Toolbox
Personal tools