Difference between revisions of "Variable"

esse quam videri
Jump to: navigation, search
(Typing)
(Definition)
 
(19 intermediate revisions by 3 users not shown)
Line 5: Line 5:
 
# an ''[[Identifier|identifier]]''
 
# an ''[[Identifier|identifier]]''
 
# ''[[Value|value]]'' (a known or unknown quantity of information)
 
# ''[[Value|value]]'' (a known or unknown quantity of information)
 +
 +
[[File:VariableVisual.jpg|400px]]
  
 
=Relevance=
 
=Relevance=
 +
* [[Field]]
 +
* [[Property]]
 +
* [[Constant]]
 +
* [[Data Type]]
 +
* [[Identifier]]
 +
* [[Keyword]]
  
 
=Explanation=
 
=Explanation=
Line 12: Line 20:
 
As the name variable implies, information may change as the program executes. However, its name, type, and location often remain fixed.  
 
As the name variable implies, information may change as the program executes. However, its name, type, and location often remain fixed.  
  
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.
+
A [[compiler|Compiler]] will replace a variable's identifier with the data location.  
  
A [[compiler|Compiler]] will replace variables' symbolic name with the data location.
+
'''Scalar''': an alternative term for a variable.
'''scalar''': an alternative term for a variable.
 
 
 
==Actions==
 
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 (computer science)|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 [[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.
 
 
 
Variables and scope:-
 
(A) 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.
 
(B) External variables: - These are variables that are external to a function on 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, retain their values even after the functions that set them have returned.
 
  
 
==Identifier==
 
==Identifier==
  
 
An identifier is the name used to reference either the the stored value or the variable itself; the variable's name can be used separately from the data it represents.  
 
An identifier is the name used to reference either the the stored value or the variable itself; the variable's name can be used separately from the data it represents.  
 
== Scope ==
 
 
 
  
 
==Typing==
 
==Typing==
 
Changing the type of data stored in a variable may change the way the data can be used. For example, in most programming languages two integers added together will produce a sum that is also an integer.  
 
Changing the type of data stored in a variable may change the way the data can be used. For example, in most programming languages two integers added together will produce a sum that is also an integer.  
  
<code>
+
<syntaxhighlight lang="csharp">
 +
class Math () {
 
a = 1;
 
a = 1;
  
Line 43: Line 38:
  
 
c = a+b; // c will be 3
 
c = a+b; // c will be 3
 +
}
  
</code>
+
</syntaxhighlight>
  
 
However, if a is a string (such as "hello"), adding it to an integer would not necessarily provide you with an integer as a result.
 
However, if a is a string (such as "hello"), adding it to an integer would not necessarily provide you with an integer as a result.
Line 57: Line 53:
 
</code>
 
</code>
  
===Strong Typing===
+
=External Links=
 
+
* [https://en.wikibooks.org/wiki/Computer_Programming/Variables Computer Programming/Variables]
==Parameters==
+
* [http://openbookproject.net/thinkcs/python/english3e/variables_expressions_statements.html Variables, Expressions and Statements (Python)]
 
+
* [https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/var C# Var]
 
 
==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 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, risks 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==
 
{{Main|Naming Conventions}}
 
{{See also|Identifier|Namespace}}
 
 
 
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 index]] variables.
 
 
 
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.
 
 
 
[[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 which 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], Retrieved July 11, 2012 [DEAD LINK]</ref><ref name="EWD1044">{{citation | author=[[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 which 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==
 
There are many types of variables, such as: static, stack-dynamic, explicit heap-dynamic, and implicit heap-dynamic.
 
===Static===
 
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++.
 
 
 
===Static-Dynamic===
 
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===
 
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===
 
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.
 
=Resources=
 
== See also ==
 
 
 
* [[Constant]]
 
* [[Data Type]]
 
* [[Identifier]]
 
* [[Keyword]]
 
 
 
==Notes==
 
 
 
 
 
==External Links==
 
* [https://en.wikibooks.org/wiki/Computer_Programming/Variables|Computer Programming/Variables]
 
* [http://openbookproject.net/thinkcs/python/english3e/variables_expressions_statements.html|Variables, Expressions and Statements (Python)]
 
  
  
{{DEFAULTSORT:Programming Language Concepts}}
 
 
[[Category:Programming Language Concepts]]
 
[[Category:Programming Language Concepts]]
 
[[Category:Data Type]]
 
[[Category:Data Type]]
 +
[[Category:Object Oriented Programming]]

Latest revision as of 15:21, 22 July 2019

Definition

In programming, a variable is comprised of:

  1. a storage location (identified by a memory address)
  2. an identifier
  3. value (a known or unknown quantity of information)

VariableVisual.jpg

Relevance

Explanation

As the name variable implies, information may change as the program executes. However, its name, type, and location often remain fixed.

A Compiler will replace a variable's identifier with the data location.

Scalar: an alternative term for a variable.

Identifier

An identifier is the name used to reference either the the stored value or the variable itself; the variable's name can be used separately from the data it represents.

Typing

Changing the type of data stored in a variable may change the way the data can be used. For example, in most programming languages two integers added together will produce a sum that is also an integer.

class Math () {
a = 1;

b= 2;

c = a+b; // c will be 3
}

However, if a is a string (such as "hello"), adding it to an integer would not necessarily provide you with an integer as a result.

a = "hello";

b= 2;

c = a+b; // c will be hello2

External Links