World Library  
Flag as Inappropriate
Email this Article

Assignment (computer science)

Article Id: WHEBN0000344807
Reproduction Date:

Title: Assignment (computer science)  
Author: World Heritage Encyclopedia
Language: English
Subject: C (programming language), Expression-oriented programming language, , , Statement (computer science)
Collection: Programming Language Concepts
Publisher: World Heritage Encyclopedia

Assignment (computer science)

In computer programming, an assignment statement sets and/or re-sets the value stored in the storage location(s) denoted by a variable name; in other words, it copies the value into the variable. In most imperative programming languages, the assignment statement (or expression) is a fundamental construct.

Today, the most commonly used notation for this basic operation has come to be x = expr (originally Superplan 1949–1951, popularized by Fortran 1957 and C) followed by x := expr (originally ALGOL 1958, popularised by Pascal),[1] although there are many other notations in use. In some languages the symbol used is regarded as an operator (meaning that the assignment has a value) while others define the 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) 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 1
  • Single assignment 2
  • Value of an assignment 3
  • Variant forms of assignment 4
    • Augmented assignment 4.1
    • Chained assignment 4.2
    • Parallel assignment 4.3
  • Assignment versus equality 5
  • Notation 6
  • See also 7
  • Notes 8
  • References 9


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.[1][4]

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.[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.

An example segment of C code:

int x = 10; 
float y;
x = 23;
y = 32.4f;

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

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 reassignment 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,[5] 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,[6] 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 the 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, OCaml, Oz, SASL, Scala (for vals), SISAL, Standard ML. 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,[8] 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 the unit type, which is represented as (). This type has only one possible value, therefore containing no information. It is typically the type of an expression that is evaluated purely for its side effects.

Variant forms of assignment

Certain use patterns are very common, and thus often have special syntax to support them. These are primarily syntactic sugar to reduce redundancy in the source code, but can also simplify compilation by clarifying the programmer's intent and easing analysis of the source code.

Augmented assignment

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, provide special operators called augmented assignment, like *=, so a = 2*a can instead be written as a *= 2.[3] Beyond syntactic sugar, this simplifies compilation, since it makes it clear that in-place modification of the variable a is ok.

Chained assignment

A statement like w = x = y = z is called a chained assignment in which the value of z is assigned to multiple variables w, x, and y. Chained assignments are often used to initialize multiple variables, as in

a = b = c = d = f = 0

Not all programming languages support chained assignment. Chained assignments are equivalent to a sequence of assignments, but the evaluation strategy differs between languages. For simple chained assignments, like initializing multiple variables, the evaluation strategy does not matter, but if the targets (l-values) in the assignment are connected in some way, the evaluation strategy affects the result.

In some programming languages (C for example), chained assignments are supported because assignments are expressions, and have values. In this case chain assignment can be implemented by having a right-associative assignment, and assignments happen right-to-left. For example, i = arr[i] = f() is equivalent to arr[i] = f(); i = arr[i]. In C++ they are also available for values of class types by declaring the appropriate return type for the assignment operator.

In Python, assignment statements are not expressions and thus do not have a value. Instead, chained assignments are a series of statements with multiple targets for a single expression. The assignments are executed left-to-right so that i = arr[i] = f() evaluates the expression f(), then assigns the result to the leftmost target, i, and then assigns the same result to the next target, arr[i], using the new value of i.[9] This is essentially equivalent to tmp = f(); i = tmp; arr[i] = tmp though no actual variable is produced for the temporary value.

Parallel assignment

Some programming languages, such as Go,[10] JavaScript (since 1.7), Maple, Lua, occam 2,[11] Perl,[12] Python,[13] REBOL, Ruby,[14] and Windows PowerShell 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. This is most often known as parallel assignment; it was introduced in CPL in 1963, under the name simultaneous assignment,[15] and is sometimes called multiple assignment, though this is confusing when used with "single assignment", as these are not opposites. If the right-hand side of the assignment is a single variable (e.g. an array or structure), the feature is called unpacking[16] or destructuring assignment:[17]

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.

Some languages, such as Go and Python, combine parallel assignment, tuples, and automatic tuple unpacking to allow multiple return values from a single function, as in this Python example:

def f():
    return 1, 2
a, b = f()

This provides an alternative to the use of output parameters for returning multiple values from a function.

In C and C++, the comma operator is similar to parallel assignment in allowing multiple assignments to occur within a single statement, writing a = 1, b = 2 instead of a, b = 1, 2. This is primarily used in for loops, and is replaced by parallel assignment in other languages such as Go.[18]

Assignment versus equality

The use of the equals sign = as an assignment operator has been frequently criticized, due to the conflict with equals as comparison for equality. This results both in confusion by novices in writing code, and confusion even by experienced programmers in reading code. The use of equals for assignment dates back to Heinz Rutishauser's language Superplan, designed from 1949 to 1951, and was particularly popularized by Fortran:

A notorious example for a bad idea was the choice of the equal sign to denote assignment. It goes back to Fortran in 1957[1] and has blindly been copied by armies of language designers. Why is it a bad idea? Because it overthrows a century old tradition to let “=” denote a comparison for equality, a predicate which is either true or false. But Fortran made it to mean assignment, the enforcing of equality. In this case, the operands are on unequal footing: The left operand (a variable) is to be made equal to the right operand (an expression). x = y does not mean the same thing as y = x.[19]
Niklaus WirthGood Ideas, Through the Looking Glass

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 some languages, such as BASIC, a single equals sign ("=") is used for both the assignment operator and the equality relational operator, with context determining which is meant. Other languages use different symbols for the two operators. For example:

  • In Pascal, the assignment operator is a colon and an equals sign (":=") while the equality operator is a single equals ("=").
  • In C, the assignment operator is a single equals sign ("=") while the equality operator is a pair of equals signs ("==").
  • In R, the assignment operator is basically <-, as in x <- value, but a single equals sign can be used in certain contexts.

The similarity in the two symbols 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 (in the same way that a function returns a value), and can be validly nested inside expressions. 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 Boolean true, in which case the then clause will be executed, leading the program to behave unexpectedly. Some language processors (such as gcc) can detect such situations, and warn the programmer of the potential error.


The two most common representations for the copying assignment are equals sign (=) and colon-equals (:=). Both forms may semantically denote either an assignment statement or an assignment operator (which also has a value), depending on language and/or usage.

variable = expression Fortran, PL/I, C (and descendants such as C++, Java, etc.), Bourne shell, Python, Go (assignment to pre-declared variables), Windows PowerShell, etc.
variable := expression ALGOL (and derivatives), CPL, BCPL, Pascal[20] (and descendants such as Modula), Mary, PL/M, Ada, Smalltalk, Eiffel,[21][22] Oberon, Dylan,[23] Seed7, Go (shorthand for declaring and defining a variable),[24] IO, AMPL, ML,[25] etc.

Other possibilities include a left arrow or a keyword, though there are other, rarer, variants:

variable << expression Magik
variable <- expression F#, OCaml, R, S
variable <<- expression R
variableexpression APL,[26] Smalltalk
variable =: expression J
LET variable = expression BASIC
let variable := expression XQuery
set variable to expression AppleScript
set variable = expression C shell
Set-Variable variable (expression) Windows PowerShell
variable : expression Macsyma, Maxima,Rebol
var variable expression mIRC scripting language

Some platforms put the expression on the left and the variable on the right:

MOVE expression TO variable COBOL
expressionvariable TI-BASIC, Casio BASIC
expression -> variable R
put expression into variable LiveCode

Some expression-oriented languages, such as Lisp[27][28] and Tcl, uniformly use prefix (or postfix) syntax for all statements, including assignment.

(setf variable expression) Common Lisp
(set! variable expression) Scheme[29][30][31]
set variable expression Tcl
expression variable ! Forth

See also


  1. ^ Use of = predates Fortran, though it was popularized by Fortran.


  1. ^ a b c Imperative Programming
  2. ^ Topics in Information Processing
  3. ^ a b c Ruediger-Marcus Flaig (2008). Bioinformatics programming in Python: a practical course for beginners. Wiley-VCH. pp. 98–99.  
  4. ^ a b c d e Crossing borders: Explore functional programming with Haskell, by Bruce Tate
  5. ^  
  6. ^ Imperative Programming Languages (IPL)
  7. ^ John C. Mitchell (2003). Concepts in programming languages. Cambridge University Press. pp. 81–82.  
  8. ^ Hudak, Paul (2000). The Haskell School of Expression: Learning Functional Programming Through Multimedia. Cambridge: Cambridge University Press.  
  9. ^
  10. ^ The Go Programming Language Specification: Assignments
  11. ^ INMOS Limited, ed. (1988). Occam 2 Reference Manual. New Jersey: Prentice Hall.  
  12. ^  
  13. ^ Lutz, Mark (2001). Python Programming Language (2 ed.). Sebastopol: O´Reilly.  
  14. ^ Thomas, David; Hunt, Andrew (2001). Programming Ruby: The Pragmatic Programmer's Guide. Upper Saddle River: Addison Wesley.  
  15. ^ D.W. Barron et al., "The main features of CPL", Computer Journal 6:2:140 (1963). full text (subscription)
  16. ^
  17. ^
  18. ^ Effective Go: for, "Finally, Go has no comma operator and ++ and -- are statements not expressions. Thus if you want to run multiple variables in a for you should use parallel assignment (although that precludes ++ and --)."
  19. ^ Niklaus Wirth. "Good Ideas, Through the Looking Glass".  
  20. ^ Moore, Lawrie (1980). Foundations of Programming with Pascal. New York: John Wiley & Sons.  
  21. ^  
  22. ^ Wiener, Richard (1996). An Object-Oriented Introduction to Computer Science Using Eiffel. Upper Saddle River, New Jersey: Prentice Hall.  
  23. ^ Feinberg, Neal; Keene, Sonya E.; Mathews, Robert O.; Withington, P. Tucker (1997). Dylan Programming. Massachusetts: Addison Wesley.  
  24. ^ The Go Programming Language Specification: short variable declarations
  25. ^ Ullman, Jeffrey D. (1998). Elements of ML Programming: ML97 Edition. Englewood Cliffs, New Jersey: Prentice Hall.  
  26. ^  
  27. ^  
  28. ^  
  29. ^ Dybvig, R. Kent (1996). The Scheme Programming Language: ANSI Scheme. New Jersey: Prentice Hall.  
  30. ^ Smith, Jerry D. (1988). Introduction to Scheme. New Jersey: Prentice Hall.  
  31. ^ Abelson, Harold; Sussman, Gerald Jay; Sussman, Julie (1996). Structure and Interpretation of Computer Programs. New Jersey: McGraw-Hill.  
This article was sourced from Creative Commons Attribution-ShareAlike License; additional terms may apply. World Heritage Encyclopedia content is assembled from numerous content providers, Open Access Publishing, and in compliance with The Fair Access to Science and Technology Research Act (FASTR), Wikimedia Foundation, Inc., Public Library of Science, The Encyclopedia of Life, Open Book Publishers (OBP), PubMed, U.S. National Library of Medicine, National Center for Biotechnology Information, U.S. National Library of Medicine, National Institutes of Health (NIH), U.S. Department of Health & Human Services, and, which sources content from all federal, state, local, tribal, and territorial government publication portals (.gov, .mil, .edu). Funding for and content contributors is made possible from the U.S. Congress, E-Government Act of 2002.
Crowd sourced content that is contributed to World Heritage Encyclopedia is peer reviewed and edited by our editorial staff to ensure quality scholarly research articles.
By using this site, you agree to the Terms of Use and Privacy Policy. World Heritage Encyclopedia™ is a registered trademark of the World Public Library Association, a non-profit organization.

Copyright © World Library Foundation. All rights reserved. eBooks from Project Gutenberg are sponsored by the World Library Foundation,
a 501c(4) Member's Support Non-Profit Organization, and is NOT affiliated with any governmental agency or department.