Revision as of 01:56, 26 April 2006 editRodasmith (talk | contribs)Extended confirmed users, Rollbackers2,063 editsm wikt:which -> wikt:that← Previous edit | Latest revision as of 20:13, 9 January 2025 edit undoNeilOnWiki (talk | contribs)Extended confirmed users562 edits Formatting.Tag: Visual edit | ||
Line 1: | Line 1: | ||
{{ |
{{Wiktionary|Variable|variable|variables}} | ||
{{Wikiversity}} | |||
In ] and ], a '''variable''' (sometimes called a '''pronumeral''') is a symbol denoting a ] or ]. In mathematics, a variable often represents an ''unknown'' quantity that has the potential to ]; in computer science, it represents a place where a quantity can be stored. Variables are often contrasted with ]s, which are known and unchanging. | |||
The term '''Variable''' is relevant to several contexts, and is especially important to mathematics and computer science. Scientists and engineers will often use ''mathematical variables'' in formulae and equations, such as {{var|''E'' {{=}} ''m'' ''c''<sup>2</sup>}}; they will also have their own special uses of the term. | |||
In other scientific fields such as ], ], and ], 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 "]s" are factors that can be altered or chosen by the scientist. For example, ] is a common environmental factor that can be controlled in laboratory experiments. "]s" or "response variables" are those that are measured and collected as ]. An independent variable is presumed to affect a dependent one. | |||
The term '''Variable''' can also occur in other contexts, such as entertainment. | |||
In ]s, variable is a ]al set of ]. A variable such as "]" would have two attributes: ] and ]. | |||
== Variable in computer science == | |||
==General overview== | |||
Variables are used in ]s. For instance, in the formula: '''x''' + 1 = 5, '''x''' is a variable which represents an "unknown" ]. In mathematics, variables are usually represented by ]s of the ], but are also represented by letters of other ]s, such as the ], as well as various other ]s. In ], variables are usually represented by ] ]s. | |||
* ], a symbolic name associated with a value and whose associated value may be changed | |||
==Why variables are useful== | |||
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 ] of ANY number: square('''x''') = '''x''' · '''x'''. | |||
== Variable in mathematics == | |||
Now, all we need to do to find the square of a number is replace ''x'' with any number we want. | |||
* ], a symbol that represents a quantity in a mathematical expression, as used in many sciences | |||
*square(''x'') = ''x'' · ''x'' = ''y'' | |||
*square(1) = 1 · 1 = 1 | |||
*square(2) = 2 · 2 = 4 | |||
*square(3) = 3 · 3 = 9 | |||
etc... | |||
== Variable in the physical and social sciences == | |||
In the above example, the variable '''x''' is a "]" 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 ] ]s without ], such changes can occur. | |||
* ], a logical set of attributes | |||
Variables in ] are also useful for this reason. The term ], as used by programmers, is much more vague than the term as used by mathematician. Variables allow the programmer to specify relationships between ]. | |||
* ], a type of astronomical star | |||
== Other kinds of variable == | |||
==Computer programming== | |||
In programming languages, a '''variable''' can be thought of as a place to store a '']'' in ]. | |||
* "]", an episode of the television series '']'' | |||
More precisely, a variable binds (associates) a '']'' (sometimes called an ''identifier'') with the location; a value in turn is stored as a data '']'' in that location so that the object can be accessed later via the variable, much like referring to someone by name. Variables in ] 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. | |||
== |
== See also == | ||
* ] | |||
Variables are denoted by ]s. | |||
{{Disambiguation}} | |||
In some programming languages, specific characters are prepended or appended to variable identifiers to indicate the variable's type. For example: | |||
* in ], the suffix <TT>$</TT> on a variable name indicates that its value is a ]; | |||
* in ], the prefixes <TT>$</TT>, <TT>@</TT>, <TT>%</TT>, and <TT>&</TT> indicate scalar, array, hash, and subroutine variables, respectively. | |||
Following is an example of variable declarations and assignments in ], ], and ]: | |||
int a = 10; | |||
string b = "hello"; | |||
''(See also ] and ].)'' | |||
===Scope and extent=== | |||
The '']'' 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 ] 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 ]. 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. ] 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 (]) aspect of a variable. Each ] 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 ]. | |||
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 ]. 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 ]s. 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 ]. Common techniques for doing so are to have different sections of a program use different ]s, or to make individual variables ''private'' through either ] or ]. | |||
Many programming languages employ a reserved value (often named '']'' or '']'') to indicate an invalid or uninitialized variable. | |||
===Typed and untyped variables=== | |||
In ] languages such as ] or ], a variable also has ''type'', meaning that only values of a given class (or set of classes) can be stored in it. In ] languages such as ] or ], it is values and not variables which carry type. ''See ].'' | |||
Typing of variables also allows ] to be resolved at compile time. | |||
Variables often store simple data like integers and literal strings, but some programming languages allow a variable to store values of other ]s as well. Such languages may also enable functions to be ]. Such functions operate like variables to represent data of multiple types. For example, a function named "<code>length</code>" may determine the length of a list. Such a <code>length</code> function may be parametric polymorphic by including a type variable in its ] since the amount of elements in the list is independent of the elements' types. | |||
===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 <TT>x</TT> 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 <TT>x</TT> can only be referred to within the <TT>addtwo</TT> function, though of course other functions can also have variables called <TT>x</TT>. | |||
===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 '']'', whose memory is automatically reclaimed when the function returns. More generally, in '']'', 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. ] 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 '']'' language, such as C#, Java, and Lisp, the runtime environment automatically "reaps" objects when extant variables can no longer refer to them. In a ''non-garbage-collected'' languages, such as C, the program (and thus the programmer) must explicitly '']'' memory and later ''free'' it to reclaim its memory. Failure to do so leads to ]s, in which the heap is depleted as the program runs, risking of eventual failure from exhausting available memory. | |||
When a variable refers to a ] 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. | |||
===Mutable vs. immutable=== | |||
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++, but not in C, "<code>mutable</code>" is a keyword to allow a mutable member to be modified by a ''const'' member function. | |||
===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. <code>const int HoursPerDay=24</code>). 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.: <code>CONST a = 60</code> | |||
;'''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.: <code>final int a = b + 20;</code>. | |||
Although such dynamic constants have fixed values if they are primitive types (e.g. <code>int</code>), their values may change if they contain a reference to an object, 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 | |||
==See also== | |||
*] | |||
==External links== | |||
*, from the United Kingdom's Sexual Offences Act of 2003 | |||
] | |||
] | |||
] | |||
] | |||
] | |||
] | |||
] | |||
] | |||
] | |||
] | |||
] | |||
] | |||
] | |||
] | |||
] | |||
] | |||
] | |||
] | |||
] | |||
] | |||
] |
Latest revision as of 20:13, 9 January 2025
The term Variable is relevant to several contexts, and is especially important to mathematics and computer science. Scientists and engineers will often use mathematical variables in formulae and equations, such as E = m c; they will also have their own special uses of the term.
The term Variable can also occur in other contexts, such as entertainment.
Variable in computer science
- Variable (computer science), a symbolic name associated with a value and whose associated value may be changed
Variable in mathematics
- Variable (mathematics), a symbol that represents a quantity in a mathematical expression, as used in many sciences
Variable in the physical and social sciences
- Variable (research), a logical set of attributes
- Variable star, a type of astronomical star
Other kinds of variable
- "The Variable", an episode of the television series Lost
See also
Topics referred to by the same term This disambiguation page lists articles associated with the title Variable.If an internal link led you here, you may wish to change the link to point directly to the intended article. Category: