Assignment (computer science)
From Wikipedia, the free encyclopedia
| This article does not cite any references or sources. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed. (May 2007) |
In computer science the assignment statement sets or re-sets the value stored in the storage location(s) denoted by a variable name. In most imperative computer programming languages the assignment statement is one of the basic statements.
The assignment statement often allows that the same variable name to contain different values at different times during program execution.
Contents |
[edit] Notation
Common textual representations of the assignment include an equals sign (“=”) and “:=”. These two forms are typical of programming languages (such as C), that classify assignment as an infix operator.
-
variable = expressionBASIC, Fortran, C, Java, Windows PowerShell, Bourne shell, … variable := expressionALGOL, Pascal, Ada, Dylan, …
Other possibilities include a left arrow or a keyword, though there are other, rarer, variants:
-
variable <- expressionObjective Caml, S, R, ... variable ← expressionAPL LET variable = expressionBASIC set variable to expressionAppleScript set variable = expressionC shell Set-Variable variable (expression)Windows PowerShell variable : expressionMacsyma, Maxima
Some platforms put the expression on the left and the variable on the right:
-
MOVE expression TO variableCOBOL expression → variableTI-BASIC, Casio BASIC expression -> variableR
Some expression-oriented languages, such as Lisp and Tcl, uniformly use prefix syntax for all statements, including assignment.
[edit] Operation
Semantically, an assignment operation modifies the current state of the executing program. Consequently, assignment is dependent on the concept of variables. In an assignment:
- The
expressionis evaluated in the current state of the program. - The
variableis assigned the computed value, replacing the prior value of that variable.
Example: Assuming that a is a numeric variable, the assignment a := 2*a means that the content of the variable a is doubled after the execution of the statement.
An example segment of C code:
int x = 10; float y; x = 23; y = 32.4;
In this sample, the variable x is first declared as an int, and is then assigned the value of 10. Notice that the declaration and assignment occur in the same statement. In the second line, y is declared without an assignment. In the third line, x is reassigned the value of 23. Finally, y is assigned the value of 32.4.
For an assignment operation, it is necessary that the value of the expression is well-defined (it is a valid rvalue) and that the variable represents a modifiable entity (it is a valid modifiable (non-const) lvalue). In some languages, such as Perl, it is not necessary to declare a variable prior to assigning it a value.
[edit] Parallel assignment
Some programming languages, such as Python, Perl, REBOL, Ruby, Windows PowerShell, and JavaScript (since 1.7), allow several variables to be assigned in parallel, with syntax like:
a,b := 0,1
which simultaneously assigns 0 to a and 1 to b. If the right-hand side of the assignment is an array variable, this feature is sometimes called sequence unpacking:
var list := 0,1
a,b := list
The list will be unpacked so that 0 is assigned to a and 1 to b. More interestingly,
a,b := b,a
Swaps the values of a and b. In languages without parallel assignment, this would have to be written to use a temporary variable
var t := a
a := b
b := t
since a:=b ; b:=a leaves both a and b with the original value of b.
Parallel assignment was introduced in CPL, in 1963, with the name 'simultaneous assignment'.[1]
[edit] Value of an assignment
In most expression-oriented programming languages, the assignment statement returns the assigned value, allowing such idioms as x = y = a, which assigns the value of a to both x and y, and while (f = read()) {…}, which uses the return value of a function to control a loop while assigning that same value to a variable.
In other programming languages, the return value of an assignment is undefined and such idioms are invalid. An example is Scheme.
In Python, assignment is not an expression, and thus has no "value".
In Haskell, there is no variable assignment; but operations similar to assignment (like assigning to a field of an array or a field of a mutable data structure) usually evaluate to unit, the value of the unit type, which is typically the type of an expression that is evaluated purely for its side effects.
[edit] Assignment versus single assignment
In functional programming, assignment is discouraged in favor of single assignment, also called name binding or initialization. Single assignment differs from assignment as described in this article in that it can only be made once, usually when the variable is created; no subsequent re-assignment is allowed. Once created by single assignment, named values are not variables but immutable objects.
Single assignment is the only form of assignment available in purely functional languages, such as Haskell, which do not have variables in the sense of imperative programming languages. Impure functional languages provide both single assignment as well as true assignment (though true assignment is used with less frequency than in imperative programming languages). For example, in Scheme, both single assignment and true assignment can be used on all variables. In OCaml, only single assignment is allowed for variables, via the let name = value syntax; however, true assignment, by a separate <- operator, can be used on elements of arrays and strings, as well as fields of records and objects that have been explicitly declared mutable (meaning capable of being changed after its initial declaration) by the programmer.
[edit] Assignment versus equality
Beginning programmers sometimes confuse assignment with the relational operator for equality, as "=" means equality in mathematics, and is used for assignment in many languages. But assignment alters the value of a variable, while equality testing tests whether two expressions have the same value.
In many languages, the assignment operator is a single equals sign ("=") while the equivalence operator is a pair of equals signs ("=="); in some languages, such as BASIC, a single equals sign is used for both, with context determining which is meant.
This can lead to errors if the programmer forgets which form (=, ==, :=) is appropriate (or mistypes = when == was intended). This is a common programming problem with languages such as C, where the assignment operator also returns the value assigned, and can be validly nested inside expressions (in the same way that a function returns a value). If the intention was to compare two values in an if statement, for instance, an assignment is quite likely to return a value interpretable as TRUE, in which case the then clause will be executed, leading the program to behave unexpectedly. Some language processors can detect such situations, and warn the programmer of the potential error.
[edit] Notes
- ^ D.W. Barron et al., "The main features of CPL", Computer Journal 6:2:140 (1963). full text (subscription)

