Lably
Skip to content

Assignment (computer science)

Setting or re-setting the value associated with a variable name

  • 14 min read
  • 6 chapters

Learn it on Lably

Today, the most commonly used notation for this operation is x = expr (originally Superplan 1949–51, popularized by Fortran 1957 and C). The second most commonly used notation is x := expr (originally ALGOL 1958, popularised by Pascal). Many other notations are also in use. In some languages, the symbol used is regarded as an operator (meaning that the assignment statement as a whole returns a value). Other languages define assignment as a statement (meaning that it cannot be used in an expression).

Assignments typically allow a variable to hold different values at different times during its life-span and scope. However, some languages (primarily strictly functional languages) do not allow that kind of "destructive" reassignment, as it might imply changes of non-local state. The purpose is to enforce referential transparency, i.e. functions that do not depend on the state of some variable(s), but produce the same results for a given set of parametric inputs at any point in time. Modern programs in other languages also often use similar strategies, although less strict, and only in certain parts, in order to reduce complexity, normally in conjunction with complementing methodologies such as data structuring, structured programming and object orientation.

Semantics

An assignment operation is a process in imperative programming in which different values are associated with a particular variable name as time passes. The program, in such model, operates by changing its state using successive assignment statements. Primitives of imperative programming languages rely on assignment to do iteration. At the lowest level, assignment is implemented using machine operations such as MOVE or STORE.

Variables are containers for values. It is possible to put a value into a variable and later replace it with a new one. An assignment operation modifies the current state of the executing program. 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.

An example segment of C code:

<span id="mwYg"></span><span class="kt" id="mwYw">int</span><span class="w" id="mwZA"> </span><span class="n" id="mwZQ">x</span><span class="w" id="mwZg"> </span><span class="o" id="mwZw">=</span><span class="w" id="mwaA"> </span><span class="mi" id="mwaQ">10</span><span class="p" id="mwag">;</span><span class="w" id="mwaw"> </span> <span class="kt" id="mwbA">float</span><span class="w" id="mwbQ"> </span><span class="n" id="mwbg">y</span><span class="p" id="mwbw">;</span> <span class="n" id="mwcA">x</span><span class="w" id="mwcQ"> </span><span class="o" id="mwcg">=</span><span class="w" id="mwcw"> </span><span class="mi" id="mwdA">23</span><span class="p" id="mwdQ">;</span> <span class="n" id="mwdg">y</span><span class="w" id="mwdw"> </span><span class="o" id="mweA">=</span><span class="w" id="mweQ"> </span><span class="mf" id="mweg">32.4f</span><span class="p" id="mwew">;</span>

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. In such languages, a variable is automatically declared the first time it is assigned to, with the scope it is declared in varying by language.

Sign in to keep reading

5 more chapters are free with a Lably account.

More in Computing and communication