Variable (computer science): Difference between revisions

Content deleted Content added
Closedmouth (talk | contribs)
m Reverted edits by 217.23.230.139 (talk) to last version by 63.170.164.6
No edit summary
Tags: Manual revert Mobile edit Mobile web edit
 
(572 intermediate revisions by more than 100 users not shown)
Line 1:
{{Short description|Named container for a particular type of data}}
{{Unreferenced|date=August 2009}}
{{distinguish|Variable (mathematics)}}
In [[computer programming]], a '''variable''' is an [[identifier]] (usually a letter, word, or phrase) that is linked to a [[value (computer science)|value]] stored in the system's [[Computer data storage|memory]] or an [[Expression (programming)|expression]] that can be evaluated. For instance, a variable might be called "<code>total_count</code>" and contain a number.
{{more citations needed|date=November 2009}}
 
In [[computer programming]], a '''variable''' is an abstract storage ___location paired with an associated [[symbol|symbolic name]], which contains some known or unknown quantity of [[Data (computer science)|data]] or [[Object (computer science)|object]] referred to as a ''[[value (computer science)|value]]''; or in simpler terms, a variable is a named container for a particular set of bits or [[Data type|type of data]] (like [[Integer (computer science)|integer]], [[Floating-point arithmetic|float]], [[String (computer science)|string]], etc...).{{Sfn|Brookshear|2019|ps=, "high-level programming languages allow locations in
In [[imperative programming|imperative]] [[programming language]]s, values can generally be [[Dereference|accessed]] or [[Assignment (computer science)|changed]] at any time. However, in [[Pure function|pure]] [[functional programming|functional]] and [[logic programming|logic language]]s, variables are [[Free variables and bound variables|bound]] to expressions and keep a single value during their entire [[Scope (programming)|lifetime]] due to the requirements of [[referential transparency (computer science)|referential transparency]]. In imperative languages, the same behavior is exhibited by [[constant (programming)|constant]]s, which are typically contrasted with normal variables.
main memory to be referenced by descriptive names rather than by numeric
addresses."|loc="Variables and Data Types"|p=249}} A variable can eventually be associated with or identified by a [[memory address]]. The variable name is the usual way to [[Reference (computer science)|reference]] the stored value, in addition to referring to the variable itself, depending on the context. This separation of name and content allows the name to be used independently of the exact information it represents. The identifier in computer [[source code]] can be [[Name binding|bound]] to a [[Value (computer science)|value]] during [[Run time (program lifecycle phase)|run time]], and the value of the variable may thus change during the course of [[Execution (computing)|program execution]].<ref>{{citation | title=[[Compilers: Principles, Techniques, and Tools]] | date=1986| pages=26–28| bibcode=1986cptt.book.....A| last1=Aho| first1=Alfred V.| last2=Sethi| first2=Ravi| last3=Ullman| first3=Jeffrey D.}}</ref><ref>{{cite book |last=Knuth |first=Donald |date=1997 |title=The Art of Computer Programming |volume=1 |edition=3rd |___location=Reading, Massachusetts |publisher=Addison-Wesley |pages=3–4 |isbn=0-201-89683-4 }}</ref><ref>{{cite web |title=Programming with variables |url=https://www.khanacademy.org/computing/ap-computer-science-principles/programming-101/storing-variables/a/assigning-variables |website=Khan Academy |access-date=23 March 2020}}</ref><ref>{{cite web |title=Scratch for Budding Coders |url=https://cs.harvard.edu/malan/scratch/variables.php |publisher=Harvard |access-date=23 March 2020 |archive-date=23 March 2020 |archive-url=https://web.archive.org/web/20200323124927/https://cs.harvard.edu/malan/scratch/variables.php |url-status=dead }}</ref>
 
Variables in programming may not directly correspond to the concept of [[Variable (mathematics)|variables in mathematics]]. The latter is [[Abstract and concrete|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.
Depending on the [[type system]] of a programming language, variables may only be able to store a specified [[datatype]] (e.g. [[Integer (computer science)|integer]] or [[string (computer science)|string]]). Alternatively a datatype may be associated only with the current value, allowing a single variable to store anything supported by the programming language.
 
A variable's storage ___location may be referenced by several different identifiers, a situation known as [[Aliasing (computing)|aliasing]]. Assigning a value to the variable using one of the identifiers will change the value that can be accessed through the other identifiers.
The use of the term "variable" is based on the similar [[Variable (mathematics)|mathematical concept of the same name]].
 
[[compiler (computing)|Compilers]] have to replace variables' symbolic names with the actual locations of the data. While a variable's name, type, and ___location often remain fixed, the data stored in the ___location may be changed during program execution.
==Naming conventions==
{{Main|Naming conventions (programming)}}
{{See also|Identifier|Namespace (computer science)}}
 
==Actions on a variable==
Unlike their mathematical counterparts, programming variables and constants commonly take multiple-character names, e.g. <code>COST</code> or <code>total</code>. Single-character names are most commonly used only for auxiliary variables; for instance, <code>i</code>, <code>j</code>, <code>k</code> for [[Array data type|array]] index variables.
In [[imperative programming|imperative]] [[programming language]]s, values can generally be [[dereference|accessed]] or [[assignment (computer science)|changed]] at any time. In [[pure function|pure]] [[functional programming|functional]] and [[logic programming|logic language]]s, variables are [[free variables and bound variables|bound]] to expressions and keep a single value during their entire [[scope (programming)|lifetime]] due to the requirements of [[referential transparency]]. In imperative languages, the same behavior is exhibited by (named) [[constant (programming)|constant]]s (symbolic constants), which are typically contrasted with (normal) variables.
 
Depending on the [[type system]] of a programming language, variables may only be able to store a specified [[data type]] (e.g. [[integer (computer science)|integer]] or [[string (computer science)|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.
In some programming languages, specific characters (known as [[sigil (computer programming)|sigil]]s) are prefixed or appended to variable identifiers to indicate the variable's type. For example:
* In [[BASIC programming language|BASIC]], the suffix <TT>$</TT> on a variable name indicates that its value is a [[string (computer science)|string]];
* In [[Perl]], the sigils <TT>$</TT>, <TT>@</TT>, <TT>%</TT>, and <TT>&</TT> indicate [[scalar (computing)|scalar]], [[array]], [[hash]], and [[subroutine]] variables, respectively.
* In [[spreadsheet]]s variables can refer to cells (e.g. <code>$A$2</code>), named ranges, or values in associated source code or functions.
 
Variables and scope:
==In source code ==
* [[Automatic variable]]s: Each local variable in a function comes into existence only when the [[Function (computer programming)|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 than 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==
In computer [[source code]], a variable name is one way to [[name binding|bind]] a variable to a memory ___location; the corresponding value is stored as a [[data object]] in that ___location so that the object can be accessed and manipulated later via the variable's name.
 
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 [[Attribute (computing)|attributes]] of the variable, such as access permission, [[Lock (computer science)|locks]], [[Semaphore (programming)|semaphores]], etc.
==In spreadsheets==
In a [[spreadsheet]], a cell may contain a [[formula]] with [[cell reference|references to other cells]]. Such a cell reference is a kind of variable; its value is the value of the referenced cell (see also: [[reference (computer science)]]).
 
For instance, a variable might be referenced by the identifier "{{code|total_count}}" and the variable can contain the number 1956. If the same variable is referenced by the identifier "{{code|r}}" as well, and if using this identifier "{{code|r}}", the value of the variable is altered to 2009, then reading the value using the identifier "{{code|total_count}}" will yield a result of 2009 and not 1956.
==Scope and extent==
 
<!-- Note: Please do not change this header, cross-referenced from [[scope (programming)]]. -->
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 "{{code|total_count}}" is the name of the variable in question, and "{{code|r}}" is another name of the same variable.
 
== {{Anchor|Scope and extent|SCOPE-AND-EXTENT}}Scope and extent ==
{{See also|Free variables and bound variables}}
The ''[[scope (programming)|scope]]'' of a variable describes where in a program's text, the variable may be used, while the ''extent'' (or ''lifetime'') describes when in a program's execution a variable has a value. The scope of a variable is actually a property of the name of the variable, and the extent is a property of the variable itself.
 
The ''[[Scope (computer science)|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 (computer science)#Overview|scope: an overview]]. Further, [[object lifetime]] may coincide with variable lifetime, but in many cases is not tied to it.
A variable name's ''scope'' affects its ''extent''.
 
''Scope'' is aan important part of the [[LexicalName variableresolution scoping#Static(programming scopinglanguages)|lexicalname resolution]] aspect of a variable. Most languages define a specific ''scope'' for each variable (as well as any other named entity), which may differ within a given program. The scope of a variable is the portion of the program's codetext 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 lifetime (as it comes into context) and exit from that scope typically ends its lifetime (as it goes out of context). For instance, a variable with "[[lexical scope]]" is meaningful only within a certain function/[[subroutine]], or more finely within a block of expressions/statements (accordingly with [[function scope]] or [[subroutineblock scope]]); this is static resolution, performable at parse-time or compile-time. Alternatively, a variable with [[dynamic scope]] is resolved at run-time, based on a global binding [[Stack (abstract data type)|stack]] that depends on the specific [[control flow]]. Variables only accessible within a certain functions are termed "[[local variable]]s". A "[[global variable]]", or one with indefinite scope, may be referred to anywhere in the program.
 
''Extent'', on the other hand, is a runtime ([[Dynamic variable scoping#Dynamic scoping|dynamic]]) aspect of a variable. Each [[Name binding (computer science)|binding]] of a variable to a value can have its own ''extent'' at runtime. The extent of the 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 (computer science)|closure]].
 
InUnless portionsthe ofprogramming codelanguage features [[garbage collection (computer science)|garbage collection]], a variable whose extent permanently outlasts its scope can result in a [[memory leak]], whereby the memory allocated for the variable can never be freed since the variable which would be used to reference it for deallocation purposes is no longer accessible. However, it can be permissible for a variable binding to extend beyond its scope, as occurs in Lisp [[closure (computer science)|closures]] and C [[static local variable]]s; when execution passes back into the variable's scope, the variable may neveronce haveagain beenbe used. A variable whose scope begins before its extent does is said to be ''[[uninitialized variable|uninitialized]]'' and often has an undefined, arbitrary value if accessed (see [[wild pointer]]), since it has yet to be explicitly given a particular value,. orA variable whose extent ends before its valuescope may havebecome a [[dangling pointer]] and deemed uninitialized once more since its value has been destroyed. SuchVariables variablesdescribed areby describedthe asprevious "two cases may be said to be ''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 [[undefined behavior|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 [[Local variable#Static local variables|static local variable]]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, butthus 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 [[action at distance (computer science)|action at a distance]]. Common techniques for doing so are to have different sections of a program use different [[namespace (programming)|namespacename space]]s, or to make individual variables "private" through either [[Dynamic variable scoping#Dynamic scoping|dynamic variable scoping]] or [[Lexical variable scoping#Static scoping|lexical variable scoping]].
 
Many programming languages employ a reserved value (often named ''[[null (computer programming)pointer|null]]'' or ''nil'') to indicate an invalid or uninitialized variable.
 
==Typing==
{{Main|Type system}}
{{See also|Datatype}}
In [[static typing|statically- typed]] languages such as [[C (programming language)|C]], [[C++]], [[Java (programming language)|Java]] or [[MLC Sharp (programming language)|MLC#]], a variable also has a ''type'', meaning that only valuescertain of a given class (or setkinds of classes)values can be stored in it. AFor variable ofexample, a primitive type holds a value of that exact primitive type. A variable of a class type can"[[integer hold(computer a null reference or a reference to an object whose typescience)|integer]]" is thatprohibited classfrom typestoring ortext any subclass of that class typevalues.<ref>{{Cite Aweb variable|date=2023-06-08 of|title=Static antyping interface- typeMDN canWeb holdDocs a null reference or a reference to anGlossary: instanceDefinitions of anyWeb-related classterms that{{!}} implementsMDN the interface|url=https://developer.mozilla.org/en-US/docs/Glossary/Static_typing A|access-date=2024-05-06 variable|website=developer.mozilla.org of an array type can hold a null reference or a reference to an array.|language=en-US}}</ref>
 
In [[dynamic typing|dynamically- typed]] languages such as [[Python (programming language)|Python]], ita variable's type is values,inferred by notits variablesvalue, whichand carrycan typechange according to its value. In [[Common Lisp]], both situations exist simultaneously: aA variable is given a type (if undeclared, it is assumed to be <{{code>|T</code>}}, the universal [[SubtypeSubtyping|supertype]]) which exists at compile time. Values also have types, which can be checked and queried at runtime.
 
Typing of variables also allows [[type polymorphism|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 value type as opposed to 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 [[datatype]]s as well. Such languages may also enable functions to be [[Parametric polymorphism#Parametric polymorphism|parametric polymorphic]]. These 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 [[type signature]], since the amountnumber of elements in the list is independent of the elements' types.
 
==Parameters==
The ''formal parameters'' (or ''formal arguments'') of functions are also referred to as variables. For instance, in this [[Python (programming language)|Python]] code segment,
 
<sourcesyntaxhighlight lang="pythonpycon">
>>> def addtwoadd_two(x):
... return x + 2
...
>>> add_two(5)
addtwo(5) # yields 7
7
</source>
</syntaxhighlight>
 
Thethe variable named <{{code>|x</code>}} is a ''[[Parameter (computer programming)|parameter]]'' because it is given a value when the function is called. The integer 5 is the ''argument'' which gives <{{code>|x</code>}} its value. In most languages, function parameters have local scope {{Citation needed|date=October 2007}}. This specific variable named <{{code>|x</code>}} can only be referred to within the <{{code>addtwo</code>|add_two}} function (though of course other functions can also have variables called <{{code>|x</code>}}).
 
==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 ''[[local variable]]s'', whose extent lasts for a single function call on the ''[[call stack]]'', and 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. [[reference (computer science)|Referencing]] is more common for variables whose values have large or unknown sizes when the code is compiled. Such variables reference the ___location of the value instead of the storing the value itself, which is allocated from a pool of memory called the ''[[Dynamic memory allocation#Heap-based memory allocation|heap]]''.
 
Bound variables have values. A value, however, is an abstraction, an idea; in implementation, a value is represented by some ''[[Object (computer science)|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 reuse when the object is no longer needed to represent some variable's value.
 
Objects allocated from the heap must be reclaimed&mdash;especially when the objects are no longer needed. In a [[garbage collection (computer science)|garbage-collected]] language (such as [[C Sharp (programming language)|C#]], [[Java (programming language)|Java]], Python, Golang and [[Lisp (programming language)|Lisp]]), the runtime environment automatically reclaims objects when extant variables can no longer refer to them. In non-garbage-collected languages, such as [[C (programming language)|C]], the program (and the programmer) must explicitly [[malloc|allocate]] memory, and then later free it, to reclaim its memory. Failure to do so leads to [[memory leak]]s, in which the heap is depleted as the program runs, riskingrisks 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 a case where only a portion of the memory reachable from the variable needs to be reclaimed.
 
==Naming conventions==
==Interpolation==<!--
{{Main|Naming conventions (programming)}}
####
{{See also|Identifier (computer languages)|Namespace}}
EDITOR NOTE: The header text for this section is directly linked by other articles.
 
Please do not change the header text unless you know what links here and can make
Unlike their mathematical counterparts, programming variables and constants commonly take multiple-character names, e.g. {{code|COST}} or {{code|total}}. Single-character names are most commonly used only for auxiliary variables; for instance, {{code|i}}, {{code|j}}, {{code|k}} for [[array index]] variables.
the appropriate fixes.
 
####
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, [[sigil (computer programming)|sigil]]s (symbols or punctuation) are affixed to variable identifiers to indicate the variable's datatype or scope.
-->
 
'''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]].
[[Case-sensitivity]] of variable names also varies between languages and some languages require the use of a certain case in naming certain entities;<ref group="note">For example, [[Haskell (programming language)|Haskell]] requires that names of types start with a capital letter.</ref> Most modern languages are case-sensitive; some older languages are not. Some languages reserve certain forms of variable names for their own internal use; in many languages, names beginning with two underscores ("__") often fall under this category.
 
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.<ref name="Choosing good variable names">[http://www.dotcadot.ca/articles/how-not-pick-variables How Not To Pick Variables] {{Webarchive|url=https://web.archive.org/web/20161221163819/http://www.dotcadot.ca/articles/how-not-pick-variables |date=2016-12-21 }}, Retrieved July 11, 2012 [DEAD LINK]</ref><ref name="EWD1044">{{citation | author=Edsger Dijkstra | author-link=Edsger Dijkstra | title = To hell with "meaningful identifiers"! | url = http://www.cs.utexas.edu/users/EWD/transcriptions/EWD10xx/EWD1044.html}}</ref>
 
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==
Languages that support variable interpolation include [[Perl]], [[PHP]], [[Ruby (programming language)|Ruby]], and most [[Unix shell]]s. In these languages, variable interpolation only occurs when the string literal is double-quoted, but not when it is single-quoted. The variables are recognized because variables start with a [[sigil (computer programming)|sigil]] (typically "$") in these languages.
{{Portal|Computer programming}}
 
* [[Control variable (programming)]]
For example, the following [[Perl]] code:
* [[Non-local variable]]
<source lang="perl">
* [[Temporary variable]]
$name = "Nancy";
* [[Variable interpolation]]
print "$name said Hello World to the crowd of people.";
* [[Scalar (mathematics)]]
</source>
produces the output:
<pre>
Nancy said Hello World to the crowd of people.
</pre>
 
==Notes==
[[Ruby (programming language)|Ruby]] uses the "#" symbol for interpolation, and lets you interpolate any expression, not just variables.
{{Reflist|group=note}}
 
==References==
{{Reflist|colwidth=30em}}
<references/>
 
=== Works cited ===
 
* {{Cite web |last= Brookshear |first= J. Glenn |date=2019 |title=Computer Science: An Overview |url=https://www.r-5.org/files/books/computers/overviews/software/Glenn_Brookshear-Computer_Science_An_Overview-EN.pdf|access-date=2024-04-01}}
{{Data types}}
 
{{DEFAULTSORT:Variable (Programming)}}
[[Category:Variable (computer programmingscience)|* ]]
[[Category:VariablesProgramming language concepts]]
[[Category:Data types]]
[[Category:Type theory]]
 
[[de:Variable (Programmierung)]]
[[ja:変数 (プログラミング)]]
[[ru:Переменная (программирование)]]