Variable (computer science)
This article needs additional citations for verification. (November 2009) |
In
Variables in programming may not directly correspond to the concept of variables in mathematics. The latter is abstract, having no reference to a physical object such as storage location. The value of a computing variable is not necessarily part of an equation or formula as in mathematics. Variables in computer programming are frequently given long names to make them relatively descriptive of their use, whereas variables in mathematics often have terse, one- or two-character names for brevity in transcription and manipulation.
A variable's storage location may be referenced by several different identifiers, a situation known as aliasing. Assigning a value to the variable using one of the identifiers will change the value that can be accessed through the other identifiers.
Actions on a variable
In
Depending on the type system of a programming language, variables may only be able to store a specified data type (e.g. integer or string). Alternatively, a datatype may be associated only with the current value, allowing a single variable to store anything supported by the programming language. Variables are the containers for storing the values.
Variables and scope:
- Automatic variables: Each local variable in a function comes into existence only when the function is called, and disappears when the function is exited. Such variables are known as automatic variables.
- External variables: These are variables that are external to a function and can be accessed by name by any function. These variables remain in existence permanently; rather that appearing and disappearing as functions are called and exited, they retain their values even after the functions that set them have returned.
Identifiers referencing a variable
An identifier referencing a variable can be used to access the variable in order to read out the value, or alter the value, or edit other attributes of the variable, such as access permission, locks, semaphores, etc.
For instance, a variable might be referenced by the identifier "total_count
" and the variable can contain the number 1956. If the same variable is referenced by the identifier "r
" as well, and if using this identifier "r
", the value of the variable is altered to 2009, then reading the value using the identifier "total_count
" will yield a result of 2009 and not 1956.
If a variable is only referenced by a single identifier, that identifier can simply be called the name of the variable; otherwise we can speak of it as one of the names of the variable. For instance, in the previous example the identifier "total_count
" is a name of the variable in question, and "r
" is another name of the same variable.
Scope and extent
The scope of a variable describes where in a program's text the variable may be used, while the extent (also called lifetime) of a variable describes when in a program's execution the variable has a (meaningful) value. The scope of a variable affects its extent. The scope of a variable is actually a property of the name of the variable, and the extent is a property of the storage location of the variable. These should not be confused with context (also called environment), which is a property of the program, and varies by point in the program's text or execution—see scope: an overview. Further, object lifetime may coincide with variable lifetime, but in many cases is not tied to it.
Scope is an important part of the
Extent, on the other hand, is a runtime (
Unless the programming language features
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, thus 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
Many programming languages employ a reserved value (often named null or nil) to indicate an invalid or uninitialized variable.
Typing
In
In
T
, the universal supertypeTyping of variables also allows
Variables often store simple data, like integers and literal strings, but some programming languages allow a variable to store values of other
length
may determine the length of a list. Such a length
function may be parametric polymorphic by including a type variable in its type signatureParameters
The formal parameters (or formal arguments) of functions are also referred to as variables. For instance, in this Python code segment,
>>> def addtwo(x):
... return x + 2
...
>>> addtwo(5)
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. 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
).
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
Bound variables have values. A value, however, is an abstraction, an idea; in implementation, a value is represented by some
Objects allocated from the heap must be reclaimed—especially when the objects are no longer needed. In a
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.
Naming conventions
Unlike their mathematical counterparts, programming variables and constants commonly take multiple-character names, e.g. COST
or total
. Single-character names are most commonly used only for auxiliary variables; for instance, i
, j
, k
for
Some naming conventions are enforced at the language level as part of the language syntax which involves the format of valid identifiers. In almost all languages, variable names cannot start with a digit (0–9) and cannot contain whitespace characters. Whether or not punctuation marks are permitted in variable names varies from language to language; many languages only permit the underscore ("_") in variable names and forbid all other punctuation. In some programming languages, sigils (symbols or punctuation) are affixed to variable identifiers to indicate the variable's datatype or scope.
However, beyond the basic restrictions imposed by a language, the naming of variables is largely a matter of style. At the machine code level, variable names are not used, so the exact names chosen do not matter to the computer. Thus names of variables identify them, for the rest they are just a tool for programmers to make programs easier to write and understand. Using poorly chosen variable names can make code more difficult to review than non-descriptive names, so names that are clear are often encouraged.[5][6]
Programmers often create and adhere to code style guidelines that offer guidance on naming variables or impose a precise naming scheme. Shorter names are faster to type but are less descriptive; longer names often make programs easier to read and the purpose of variables easier to understand. However, extreme verbosity in variable names can also lead to less comprehensible code.
Variable types (based on lifetime)
We can classify variables based on their lifetime. The different types of variables are static, stack-dynamic, explicit heap-dynamic, and implicit heap-dynamic. A static variable is also known as global variable, it is bound to a memory cell before execution begins and remains to the same memory cell until termination. A typical example is the static variables in C and C++. A Stack-dynamic variable is known as local variable, which is bound when the declaration statement is executed, and it is deallocated when the procedure returns. The main examples are local variables in C subprograms and Java methods. Explicit Heap-Dynamic variables are nameless (abstract) memory cells that are allocated and deallocated by explicit run-time instructions specified by the programmer. The main examples are dynamic objects in C++ (via new and delete) and all objects in Java. Implicit Heap-Dynamic variables are bound to heap storage only when they are assigned values. Allocation and release occur when values are reassigned to variables. As a result, Implicit heap-dynamic variables have the highest degree of flexibility. The main examples are some variables in JavaScript, PHP and all variables in APL.
See also
- Control variable (programming)
- Non-local variable
- Temporary variable
- Variable interpolation
- Scalar (mathematics)
Notes
- Haskellrequires that names of types start with a capital letter.
References
- ^ Compilers: Principles, Techniques, and Tools, pp. 26–28
- ISBN 0-201-89683-4.
- ^ "Programming with variables". Khan Academy. Retrieved 23 March 2020.
- ^ "Scratch for Budding Coders". Harvard. Retrieved 23 March 2020.
- ^ How Not To Pick Variables, Retrieved July 11, 2012 [DEAD LINK]
- Edsger Dijkstra, To hell with "meaningful identifiers"!