In computer programming, an 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, assignment statements are one of the basic statements.
Assignment statements typically allow for the same variable to contain different values at different times during program execution. Thus, languages featuring destructive assignment rarely enforce referential transparency, which requires a procedure to return the same results for a given set of inputs at any point in time.
Semantics
An assignment operation is a process in imperative programming in which different values are associated with a particular variable name as time passes.[2] The program, in such model, operates by changing its state using successive assignment statements.[1][3] Primitives of imperative programming languages rely on assignment to do iteration.[4] At the lowest level, assignment is implemented using machine operations such as MOVE or STORE.
The variables are containers for values. It is possible to put a value into variable and later replace it with a new one. An assignment operation modifies the current state of the executing program.[3] Consequently, assignment is dependent on the concept of variables. In an assignment:
The expression is evaluated in the current state of the program.
The variable is 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. The case that the assigned value depends on previous one is so common that many imperative languages, most notably C and the majority of its descendants, augment the notion of assignment by defining special binary operators like *= for convenience so that the example becomes a *= 2.[3]
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, typically dynamic ones, it is not necessary to declare a variable prior to assigning it a value.
Single assignment
In pure functional programming, destructive assignment is not allowed, because of side effects[5]
Any assignment that changes an existing value (e.g. x := x + 1) is disallowed in purely functional languages.[4] In functional programming, assignment is discouraged in favor of single assignment, also called initialization. Single assignment is an example of name binding and differs from assignment as described in this article in that it can only be done 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.
An evaluation of expression does not have a side effect if it does not change an observable state of the machine,[6] and produces same values for same input.[4] Imperative assignment can introduce side effects while destroying and making the old value unavailable while substituting it with a new one,[5] and is referred to as "destructive assignment" for that reason in LISP and functional programming, similar to destructive updating.
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[4] but rather named constant values possibly of compound nature with their elements progressively defined on-demand. Purely functional languages can provide an opportunity for computation to be performed in parallel, avoiding von Neumann bottleneck of sequential one step at time execution, since values are independent of each other.[7]
Impure functional languages provide both single assignment as well as true assignment (though true assignment is typically used with less frequency than in imperative programming languages). For example, in Scheme, both single assignment (with let) and true assignment (with set!) can be used on all variables, and specialized primitives are provided for destructive update inside lists, vectors, strings, etc. In OCaml, only single assignment is allowed for variables, via the let name = value syntax; however destructive update can be used on elements of arrays and strings with separate <- operator, as well as on fields of records and objects that have been explicitly declared mutable (meaning capable of being changed after their initial declaration) by the programmer.
Functional programming languages that use single assignment include Clojure, Erlang, F#, Haskell, Lava, Objective Caml, Oz, SASL, Scala (for vals), SISAL. Non-backtracking Prolog code can be considered explicit single-assignment, explicit in a sense that its (named) variables can be in explicitly unassigned state, or be set exactly once. In Haskell, by contrast, there can be no unassigned variables, and every variable can be thought of as being implicitly set to its value (or rather to a computational object that will produce its value on demand) when it is created.
Value of an assignment
In some programming languages, an assignment statement returns a value, while in others it does not.
In most expression-oriented programming languages (for example, C), the assignment statement returns the assigned value, allowing such idioms as x = y = a, in which the assignment statement y = a returns the value of a, which is then assigned to x. In a statement such as while (f = read()) {…}, the return value of a function is used to control a loop while assigning that same value to a variable.
In other programming languages, Scheme for example, the return value of an assignment is undefined and such idioms are invalid.
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.