Microsoft Store
 

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.

Computer programming

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

Related Topics:
Value - 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.

Related Topics:
Name binding - Referencing

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

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.

Related Topics:
Datatype - Parametric polymorphic - Type signature

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

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.

~ ~ ~ ~ ~ ~ ~ ~ ~ ~

~ ~ ~ ~ ~ ~ ~ ~ ~ ~