Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Assignment (computer science)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{Short description|Setting or re-setting the value associated with a variable name}} {{for|assignment of letters to disk file systems|Drive letter assignment}} In [[computer programming]], an '''assignment statement''' sets and/or re-sets the [[value (computer science)|value]] stored in the storage location(s) denoted by a [[variable (programming)|variable]] [[name]]; in other words, it copies a value into the variable. In most [[imperative programming|imperative]] [[programming language]]s, the assignment statement (or expression) is a fundamental construct. Today, the most commonly used notation for this operation is <code>''x'' = ''expr''</code> (originally [[Superplan]] 1949–51, popularized by [[Fortran]] 1957 and [[C (programming language)|C]]). The second most commonly used notation is<ref name="2CS24" /> <code>''x'' := ''expr''</code> (originally [[ALGOL]] 1958, popularised by [[Pascal (programming language)|Pascal]]).<ref name="weisskop">{{cite web|url=http://www.cs.uah.edu/~weisskop/Notes424-524/ch12.ppt|title=Imperative Programming|website=uah.edu|access-date=20 April 2018|archive-date=4 March 2016|archive-url=https://web.archive.org/web/20160304035233/http://www.cs.uah.edu/~weisskop/Notes424-524/ch12.ppt|url-status=dead}}</ref> Many other notations are also in use. In some languages, the symbol used is regarded as an [[operator (programming)|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 (programming)|scope]]. However, some languages (primarily [[Functional programming|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 structures|data structuring]], [[structured programming]] and [[object-oriented programming|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.<ref name="2CS24">{{cite web|url=http://www.csc.liv.ac.uk/~frans/OldLectures/2CS24/declarative.html|title=2cs24 Declarative|website=www.csc.liv.ac.uk|access-date=20 April 2018|archive-url=https://web.archive.org/web/20060424045449/http://www.csc.liv.ac.uk/~frans/OldLectures/2CS24/declarative.html|archive-date=24 April 2006|url-status=dead}}</ref> The program, in such model, operates by changing its state using successive assignment statements.<ref name="weisskop"/><ref name="Flaig2008">{{cite book |author=Ruediger-Marcus Flaig |title=Bioinformatics programming in Python: a practical course for beginners |url=https://books.google.com/books?id=kS7Dye-dv54C&pg=PA98|access-date=25 December 2010 |year=2008 |publisher=Wiley-VCH |isbn=978-3-527-32094-3 |pages=98–99}}</ref> Primitives of imperative programming languages rely on assignment to do [[iteration#Computing|iteration]].<ref name="Bruce Tate"/> At the lowest level, assignment is implemented using [[assembly language|machine operations]] such as <code>MOVE</code> or <code>STORE</code>.<ref name="weisskop"/><ref name="Bruce Tate">[http://www.ibm.com/developerworks/java/library/j-cb07186.html Crossing borders: Explore functional programming with Haskell] {{webarchive |url=https://web.archive.org/web/20101119190821/http://www.ibm.com/developerworks/java/library/j-cb07186.html |date=November 19, 2010 }}, by Bruce Tate</ref> [[variable (programming)|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.<ref name="Flaig2008"/> Consequently, assignment is dependent on the concept of [[variable (programming)|variables]]. In an assignment: * The <code>''expression''</code> is evaluated in the current state of the program. * The <code>''variable''</code> is assigned the computed value, replacing the prior value of that variable. Example: Assuming that <code>a</code> is a numeric variable, the assignment <code>a := 2*a</code> means that the content of the variable <code>a</code> is doubled after the execution of the statement. An example segment of [[C (programming language)|C]] code: <syntaxhighlight lang="c"> int x = 10; float y; x = 23; y = 32.4f; </syntaxhighlight> In this sample, the variable <code>x</code> 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, <code>y</code> is declared without an assignment. In the third line, <code>x</code> is reassigned the value of 23. Finally, <code>y</code> is assigned the value of 32.4. For an assignment operation, it is necessary that the value of the <code>''expression''</code> is well-defined (it is a valid [[value (computer science)|rvalue]]) and that the <code>''variable''</code> represents a modifiable entity (it is a valid modifiable (non-[[const]]) [[Value (computer science)|lvalue]]). In some languages, typically [[Dynamic programming language|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. ==Single assignment== {{See also|Static single-assignment form}} Any assignment that changes an existing value (e.g. <code>x := x + 1</code>) is disallowed in [[purely functional programming|purely functional]] languages.<ref name="Bruce Tate"/> In [[functional programming]], assignment is discouraged in favor of single assignment, more commonly known as ''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. An evaluation of an expression does not have a [[side effect (computer science)|side effect]] if it does not change an observable state of the machine,<ref name="Mitchell2003">{{cite book |last=Mitchell |first=John C. |author-link=John C. Mitchell |title=Concepts in programming languages |url=https://books.google.com/books?id=7Uh8XGfJbEIC&pg=PA23|access-date=3 January 2011 |year=2003 |publisher=Cambridge University Press |isbn=978-0-521-78098-8 |page=23}}</ref> other than producing the result, and always produces same value for the same input.<ref name="Bruce Tate"/> Imperative assignment can introduce side effects while destroying and making the old value unavailable while substituting it with a new one,<ref name="csci210">{{cite web |title=Imperative Programming Languages (IPL) |url=http://www.seas.gwu.edu/~bell/csci210/lectures/imperative_languages.pdf |archive-url=https://web.archive.org/web/20110716201414/http://www.seas.gwu.edu/~bell/csci210/lectures/imperative_languages.pdf |archive-date=2011-07-16 |access-date=20 April 2018 |website=gwu.edu}}</ref> and is referred to as ''destructive assignment'' for that reason in [[LISP]] and [[functional programming]], similar to [[destructive update|destructive updating]]. Single assignment is the only form of assignment available in purely functional languages, such as [[Haskell (programming language)|Haskell]], which do not have variables in the sense of imperative programming languages<ref name="Bruce Tate"/> but rather named constant values possibly of compound nature, with their elements progressively defined ''on-demand'', for the [[lazy evaluation|lazy]] languages. Purely functional languages can provide an opportunity for [[parallel computing|computation to be performed in parallel]], avoiding the [[von Neumann bottleneck]] of sequential one step at a time execution, since values are independent of each other.<ref name="Mitchell2003.1">{{cite book |author=John C. Mitchell |title=Concepts in programming languages |url=https://books.google.com/books?id=7Uh8XGfJbEIC&pg=PA81|access-date=3 January 2011 |year=2003 |publisher=Cambridge University Press |isbn=978-0-521-78098-8 |pages=81–82}}</ref> 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 <code>let</code>) and true assignment (with <code>set!</code>) 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 <code>let ''name'' = ''value''</code> syntax; however destructive update can be used on elements of arrays and strings with separate <code><-</code> 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]] (for data structures, not vars), [[Erlang (programming language)|Erlang]] (it accepts multiple assignment if the values are equal, in contrast to Haskell), [[F Sharp (programming language)|F#]], [[Haskell (programming language)|Haskell]], [[JavaScript]] (for constants), Lava, [[OCaml]], [[Oz (programming language)|Oz]] (for dataflow variables, not cells), [[Racket (programming language)|Racket]] (for some data structures like lists, not symbols), [[SASL (programming language)|SASL]], [[Scala (programming language)|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, when it is created, to its value (or rather to a computational object that will produce its value ''on demand''). ==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 (programming language)|C]]), the assignment statement returns the assigned value, allowing such idioms as <code>x = y = a</code>, in which the assignment statement <code>y = a</code> returns the value of <code>a</code>, which is then assigned to <code>x</code>. In a statement such as {{code|2=c|1=while ((ch = getchar()) != EOF) {…} }}, 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 (programming language)|Scheme]] for example, the return value of an assignment is undefined and such idioms are invalid. In [[Haskell (programming language)|Haskell]],<ref name="haskell">{{cite book |last=Hudak |first=Paul |year=2000 |title=The Haskell School of Expression: Learning Functional Programming Through Multimedia |location=Cambridge |publisher=Cambridge University Press |isbn=0-521-64408-9}}</ref> 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 <code>()</code>. 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 also assists readers of the code in understanding the programmer's intent, and provides the compiler with a clue to possible optimization. ===Augmented assignment=== {{main|Augmented assignment}} The case where the assigned value depends on a previous one is so common that many imperative languages, most notably [[C (programming language)|C]] and the majority of its descendants, provide special operators called [[augmented assignment]], like <code>*=</code>, so <code>a = 2*a</code> can instead be written as <code>a *= 2</code>.<ref name="Flaig2008"/> Beyond syntactic sugar, this assists the task of the compiler by making clear that in-place modification of the variable <code>a</code> is possible. ===Chained assignment=== A statement like <code>w = x = y = z</code> is called a '''chained assignment''' in which the value of <code>z</code> is assigned to multiple variables <code>w, x,</code> and <code>y</code>. Chained assignments are often used to initialize multiple variables, as in <code>a = b = c = d = f = 0</code> 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 (programming language)|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 [[Operator associativity#Right-associativity of assignment operators|right-associative assignment]], and assignments happen right-to-left. For example, <code>i = arr[i] = f()</code> is equivalent to <code>arr[i] = f(); i = arr[i]</code>. In [[C++]] they are also available for values of class types by declaring the appropriate return type for the assignment operator. In [[Python (programming language)|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 <code>i = arr[i] = f()</code> evaluates the expression <code>f()</code>, then assigns the result to the leftmost target, <code>i</code>, and then assigns the same result to the next target, <code>arr[i]</code>, using the new value of <code>i</code>.<ref>{{cite web|url=https://docs.python.org/reference/simple_stmts.html#assignment-statements|title=7. Simple statements — Python 3.6.5 documentation|website=docs.python.org|access-date=20 April 2018}}</ref> This is essentially equivalent to <code>tmp = f(); i = tmp; arr[i] = tmp</code> though no actual variable is produced for the temporary value. ===Parallel assignment=== Some programming languages, such as [[APL (programming language)|APL]], [[Common Lisp]],<ref>{{cite web |title=CLHS: Macro SETF, PSETF |url=http://www.lispworks.com/documentation/HyperSpec/Body/m_setf_.htm#psetf |website=Common Lisp Hyperspec |publisher=LispWorks |access-date=23 April 2019}}</ref> [[Go (programming language)|Go]],<ref>The Go Programming Language Specification: [http://golang.org/ref/spec#Assignments Assignments]</ref> [[JavaScript]] (since 1.7), [[Julia (programming language)|Julia]], [[PHP]], [[Maple (software)|Maple]], [[Lua (programming language)|Lua]], [[occam (programming language)#occam 2|occam 2]],<ref name="occam">{{cite book |editor=INMOS Limited |year=1988 |title=Occam 2 Reference Manual |location=New Jersey |publisher=Prentice Hall |isbn=0-13-629312-3}}</ref> [[Perl]],<ref name="perl">{{cite book |last=Wall |first=Larry |author-link=Larry Wall |author2=Christiansen, Tom |author3=Schwartz, Randal C. |year=1996 |edition=2 |title=Perl Programming Language |location=Cambridge |publisher=O´Reilly |isbn=1-56592-149-6 |url=https://archive.org/details/programmingperl00wall }}</ref> [[Python (programming language)|Python]],<ref name="python">{{cite book |last=Lutz |first=Mark |year=2001 |edition=2 |title=Python Programming Language |location=Sebastopol |publisher=O´Reilly |isbn=0-596-00085-5 |url=https://archive.org/details/programmingpytho00lutz }}</ref> [[REBOL]], [[Ruby (programming language)|Ruby]],<ref name="ruby">{{cite book |last=Thomas |first=David |author2=Hunt, Andrew |year=2001 |title=Programming Ruby: The Pragmatic Programmer's Guide |location=Upper Saddle River |publisher=Addison Wesley |isbn=0-201-71089-7 |url=https://archive.org/details/programmingruby000thom }}</ref> and [[PowerShell]] allow several variables to be assigned in parallel, with syntax like: a, b := 0, 1 which simultaneously assigns 0 to <code>a</code> and 1 to <code>b</code>. This is most often known as '''parallel assignment'''; it was introduced in [[CPL (programming language)|CPL]] in 1963, under the name '''simultaneous assignment''',<ref>D.W. Barron ''et al.'', "The main features of CPL", ''Computer Journal'' '''6''':2:140 (1963). [https://archive.today/20120707200431/http://comjnl.oxfordjournals.org/cgi/reprint/6/2/134 full text (subscription)]</ref> 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'''<ref>{{cite web|url=http://legacy.python.org/dev/peps/pep-3132/|title=PEP 3132 -- Extended Iterable Unpacking|website=legacy.python.org|access-date=20 April 2018}}</ref> or '''destructuring assignment''':<ref>{{cite web|url=https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment|title=Destructuring assignment|website=MDN Web Docs|access-date=20 April 2018}}</ref> '''var''' list := {0, 1} a, b := list The list will be unpacked so that 0 is assigned to <code>a</code> and 1 to <code>b</code>. Furthermore, a, b := b, a swaps the values of <code>a</code> and <code>b</code>. In languages without parallel assignment, this would have to be written to use a temporary variable '''var''' t := a a := b b := t since <code>a := b; b := a</code> leaves both <code>a</code> and <code>b</code> with the original value of <code>b</code>. Some languages, such as [[Go (programming language)|Go]], [[F Sharp (programming language)|F#]] and [[Python (programming language)|Python]], combine parallel assignment, tuples, and automatic [[b:Python Programming/Tuples#Packing and Unpacking|tuple unpacking]] to allow multiple return values from a single function, as in this Python example, <syntaxhighlight lang="python"> def f(): return 1, 2 a, b = f() </syntaxhighlight> while other languages, such as [[C sharp (programming language)|C#]] and [[Rust (programming language)|Rust]], shown here, require explicit tuple construction and deconstruction with parentheses: <syntaxhighlight lang="csharp"> // Valid C# or Rust syntax (a, b) = (b, a); </syntaxhighlight> <syntaxhighlight lang="csharp"> // C# tuple return (string, int) f() => ("foo", 1); var (a, b) = f(); </syntaxhighlight> <syntaxhighlight lang="rust"> // Rust tuple return let f = || ("foo", 1); let (a, b) = f(); </syntaxhighlight> This provides an alternative to the use of [[output parameter]]s for returning multiple values from a function. This dates to [[CLU (programming language)|CLU]] (1974), and CLU helped popularize parallel assignment generally. C# additionally allows generalized ''deconstruction assignment'' with implementation defined by the expression on the right-hand side, as the compiler searches for an appropriate [[instance method|instance]] or [[extension method|extension]] <code>Deconstruct</code> method on the expression, which must have output parameters for the variables being assigned to.<ref>{{cite web |title=Deconstructing tuples and other types |url=https://docs.microsoft.com/en-us/dotnet/csharp/deconstruct |website=Microsoft Docs |publisher=Microsoft |access-date=29 August 2019}}</ref> For example, one such method that would give the [[class (computer programming)|class]] it appears in the same behavior as the return value of <code>f()</code> above would be <syntaxhighlight lang="csharp"> void Deconstruct(out string a, out int b) { a = "foo"; b = 1; } </syntaxhighlight> In C and C++, the [[comma operator]] is similar to parallel assignment in allowing multiple assignments to occur within a single statement, writing <code>a = 1, b = 2</code> instead of <code>a, b = 1, 2</code>. This is primarily used in [[Comma operator#For loops|for loops]], and is replaced by parallel assignment in other languages such as Go.<ref>[http://golang.org/doc/effective_go.html Effective Go]: [http://golang.org/doc/effective_go.html#for 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 --)."</ref> However, the above C++ code does not ensure perfect simultaneity, since the right side of the following code <code>a = b, b = a+1</code> is evaluated after the left side. In languages such as Python, <code>a, b = b, a+1</code> will assign the two variables concurrently, using the initial value of a to compute the new b. ==Assignment versus equality== {{See also|Relational operator#Confusion with assignment operators}} The use of the equals sign <code>=</code> 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: {{blockquote|A notorious example for a bad idea was the choice of the equal sign to denote assignment. It goes back to Fortran in 1957{{efn|1=Use of <code>=</code> predates Fortran, though it was popularized by Fortran.}} 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 <nowiki>“=”</nowiki> 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). <nowiki>x = y</nowiki> does not mean the same thing as <nowiki>y = x</nowiki>.<ref>{{cite web |citeseerx=10.1.1.88.8309 |author=Niklaus Wirth |title=Good Ideas, Through the Looking Glass |url=https://citeseerx.ist.psu.edu/doc/10.1.1.88.8309 }}</ref>|[[Niklaus Wirth]]|''Good Ideas, Through the Looking Glass''}} Beginning programmers sometimes confuse assignment with the [[relational operator]] for equality, as "=" means [[equality (mathematics)|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 (<code>"="</code>) 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.<ref>{{Cite web |date=2013-06-01 |title=C++ Programming Language. Basics |url=https://www3.ntu.edu.sg/home/ehchua/programming/cpp/cp1_Basics.html#zz-3.6 |access-date=2024-06-21 |website=ntu.edu.sg}}</ref> For example: * In [[ALGOL]] and [[Pascal (programming language)|Pascal]], the assignment operator is a colon and an equals sign (<code>":="</code>) while the equality operator is a single equals (<code>"="</code>). * In [[C (programming language)|C]], the assignment operator is a single equals sign (<code>"="</code>) while the equality operator is a pair of equals signs (<code>"=="</code>). * In [[R (programming language)|R]], the assignment operator is basically <code><-</code>, as in <code>x <- value</code>, 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 ("<code>=</code>", "<code>==</code>", "<code>:=</code>") is appropriate, or mistypes "<code>=</code>" when "<code>==</code>" was intended. This is a common programming problem with languages such as C (including one famous attempt to backdoor the Linux kernel),<ref>{{cite web |author=Corbet |date=6 November 2003 |title=An attempt to backdoor the kernel |url=https://lwn.net/Articles/57135/ |access-date=2024-06-21 |website=lwn.net}}</ref> 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 <code>if</code> statement, for instance, an assignment is quite likely to return a value interpretable as Boolean true, in which case the <code>then</code> clause will be executed, leading the program to behave unexpectedly. Some language processors (such as [[GNU Compiler Collection|gcc]]) can detect such situations, and warn the programmer of the potential error.<ref>{{Cite web |title=Static Analyzer Options (Using the GNU Compiler Collection (GCC)) |url=https://gcc.gnu.org/onlinedocs/gcc/Static-Analyzer-Options.html |access-date=2024-06-21 |website=gcc.gnu.org}}</ref><ref>{{Cite web |last=Deitel |first=Paul |last2=Deitel |first2=Harvey |date=2022-10-25 |title=C++ Control Statements, Part 2 |url=https://domyassignments.com/ |access-date=2024-06-21 |website=Domyassignments}}</ref> ==Notation== {{see also|Comparison of programming languages (variable and constant declarations)}} The two most common representations for the copying assignment are [[equals sign]] (<code>=</code>) and colon-equals (<code>:=</code>). Both forms may semantically denote either an assignment ''statement'' or an assignment ''[[operator (programming)|operator]]'' (which also has a value), depending on language and/or usage. :{| class="wikitable" |- | <code>''variable'' = ''expression''</code> || [[Fortran]], [[PL/I]], [[C (programming language)|C]] (and [[:Category:C programming language family|descendants]] such as [[C++]], [[Java (programming language)|Java]], etc.), [[Bourne shell]], [[Python (programming language)|Python]], [[Go (programming language)|Go]] (assignment to pre-declared variables), [[R (programming language)|R]], [[PowerShell]], [[Nim (programming language)|Nim]], etc. |- | <code>''variable'' := ''expression''</code> || [[ALGOL]] (and derivatives), [[Simula]], [[CPL (programming language)|CPL]], [[BCPL]], [[Pascal (programming language)|Pascal]]<ref name=pascal>{{cite book |last=Moore |first=Lawrie |year=1980 |title=Foundations of Programming with Pascal |location=New York |publisher=John Wiley & Sons |isbn=0-470-26939-1}}</ref> (and descendants such as [[Modula]]), [[Mary (programming language)|Mary]], [[PL/M]], [[Ada (programming language)|Ada]], [[Smalltalk]], [[Eiffel (programming language)|Eiffel]],<ref name=eiffel1>{{cite book |last=Meyer |first=Bertrand |author-link=Bertrand Meyer |year=1992 |title=Eiffel the Language |location=Hemel Hempstead |publisher=Prentice Hall International(UK) |isbn=0-13-247925-7}}</ref><ref name=eiffel2>{{cite book |last=Wiener |first=Richard |year=1996 |title=An Object-Oriented Introduction to Computer Science Using Eiffel |location=Upper Saddle River, New Jersey |publisher=Prentice Hall |isbn=0-13-183872-5}}</ref> [[Oberon (programming language)|Oberon]], [[Dylan (programming language)|Dylan]],<ref name=dylan>{{cite book |last=Feinberg |first=Neal |author2=Keene, Sonya E.|author3= Mathews, Robert O.|author4= Withington, P. Tucker |year=1997 |title=Dylan Programming |location=Massachusetts |publisher=Addison Wesley |isbn=0-201-47976-1}}</ref> [[Seed7]], [[Python (programming language)|Python]] (an assignment expression),<ref>{{cite web|url=https://www.python.org/dev/peps/pep-0572/|title=PEP 572 – Assignment Expressions|date=28 February 2018|website=python.org|access-date=4 March 2020}}</ref> [[Go (programming language)|Go]] (shorthand for declaring and defining a variable),<ref>{{cite web|url=http://golang.org/ref/spec#Short_variable_declarations|title=The Go Programming Language Specification - The Go Programming Language|website=golang.org|access-date=20 April 2018}}</ref> [[Io (programming language)|Io]], [[AMPL]], [[ML (programming language)|ML]] (assigning to a reference value),<ref name="ml">{{cite book |last=Ullman |first=Jeffrey D. |year=1998 |title=Elements of ML Programming: ML97 Edition |location=Englewood Cliffs, New Jersey |publisher=Prentice Hall |isbn=0-13-790387-1}}</ref> [[AutoHotkey]] etc. |- |} Other possibilities include a left arrow or a keyword, though there are other, rarer, variants: :{| class="wikitable" |- | <code>''variable'' << ''expression''</code> || [[Magik (programming language)|Magik]] |- | <code>''variable'' <- ''expression''</code> || [[F Sharp (programming language)|F#]], [[OCaml]], [[R (programming language)|R]], [[S (programming language)|S]] |- | <code>''variable'' <<- ''expression''</code> || [[R (programming language)|R]] |- | <code> assign("''variable''", ''expression'')</code> || [[R (programming language)|R]] |- | <code>''variable'' ← ''expression''</code> || [[APL (programming language)|APL]],<ref name="aplbook">{{cite book |last=Iverson |first=Kenneth E. |author-link=Kenneth E. Iverson |title=A Programming Language |publisher=John Wiley and Sons |year=1962 |isbn=0-471-43014-5 |url=http://www.softwarepreservation.org/projects/apl/book/APROGRAMMING%20LANGUAGE/view |access-date=2010-05-09 |archive-url=https://web.archive.org/web/20090604091725/http://www.softwarepreservation.org/projects/apl/book/APROGRAMMING%20LANGUAGE/view |archive-date=2009-06-04 |url-status=dead }}</ref> [[Smalltalk]], [[BASIC Programming|Atari 2600 BASIC Programming]] |- | <code>''variable'' =: ''expression''</code> || [[J (programming language)|J]] |- | <code>LET ''variable'' = ''expression''</code> || [[BASIC]] |- | <code>let ''variable'' := ''expression''</code> || [[XQuery]] |- | <code>set ''variable'' to ''expression''</code> || [[AppleScript]] |- | <code>set ''variable'' = ''expression''</code> || [[C shell]] |- | <code>Set-Variable ''variable'' ''(expression)''</code> || [[PowerShell]] |- | <code>''variable'' : ''expression''</code> || [[Macsyma|Macsyma, Maxima]], [[K (programming language)|K]] |- | <code>''variable'': ''expression''</code> || [[Rebol]] |- | <code>var ''variable'' ''expression''</code> || [[mIRC scripting language]] |- | <code>''reference-variable'' :- ''reference-expression''</code> || [[Simula]] |} Mathematical [[pseudocode#Common mathematical symbols|pseudo code]] assignments are generally depicted with a left-arrow. Some platforms put the expression on the left and the variable on the right: :{| class="wikitable" |- | style="padding-right: 1em" | <code>MOVE ''expression'' TO ''variable''</code> || [[COBOL]] |- | style="padding-right: 1em" | <code>''expression'' → ''variable''</code> || [[TI-BASIC]], [[Casio BASIC]] |- | style="padding-right: 1em" | <code>''expression'' -> ''variable''</code> || [[POP-2]], [[BETA (programming language)|BETA]], [[R (programming language)|R]] |- | style="padding-right: 1em" | <code>put ''expression'' into ''variable''</code> || [[HyperTalk]], [[LiveCode]] |- | style="padding-right: 1em" | <code>PUT ''expression'' IN ''variable''</code> || [[ABC (programming language)|ABC]] |} Some expression-oriented languages, such as [[Lisp (programming language)|Lisp]]<ref name="clisp">{{cite book |last=Graham |first=Paul |author-link=Paul Graham (computer programmer) |year=1996 |title=ANSI Common Lisp |location=New Jersey |publisher=Prentice Hall |isbn=0-13-370875-6 |url=https://archive.org/details/ansicommonlisp00grah }}</ref><ref name="cmlisp">{{cite book |last=Steele |first=Guy L. |author-link= Guy L. Steele, Jr. |year=1990 |title=Common Lisp: The Language |location=Lexington |publisher=Digital Press |isbn=1-55558-041-6}}</ref> and Tcl, uniformly use prefix (or postfix) syntax for all statements, including assignment. :{| class="wikitable" |- | style="padding-right: 1em" | <code>(setf ''variable'' ''expression'')</code> || [[Common Lisp]] |- | <code>(set! ''variable'' ''expression'')</code> || [[Scheme (programming language)|Scheme]]<ref name="scheme">{{cite book |last=Dybvig |first=R. Kent |year=1996 |title=The Scheme Programming Language: ANSI Scheme |location=New Jersey |publisher=Prentice Hall |isbn=0-13-454646-6}}</ref><ref name="schemeint">{{cite book |last=Smith |first=Jerry D. |year=1988 |title=Introduction to Scheme |location=New Jersey |publisher=Prentice Hall |isbn=0-13-496712-7}}</ref><ref name="sussman">{{cite book |last=Abelson |first=Harold |author2=Sussman, Gerald Jay|author3= Sussman, Julie |year=1996 |title=Structure and Interpretation of Computer Programs |location=New Jersey |publisher=McGraw-Hill |isbn=0-07-000484-6}}</ref> |- | <code>set ''variable'' ''expression''</code> || [[Tcl]] |- | style="padding-right: 1em" | <code>''expression'' ''variable'' !</code> || [[Forth (programming language)|Forth]] |} ==See also== * [[Assignment operator (C++)]] * [[Unification (computer science)]] * [[Immutable object]] * [[Assignment problem]] ==Notes== {{notelist}} ==References== {{reflist|2}} [[Category:Programming language concepts]] [[Category:Assignment operations]] [[Category:Articles with example C code]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:Blockquote
(
edit
)
Template:Cite book
(
edit
)
Template:Cite web
(
edit
)
Template:Code
(
edit
)
Template:For
(
edit
)
Template:Main
(
edit
)
Template:Notelist
(
edit
)
Template:Reflist
(
edit
)
Template:See also
(
edit
)
Template:Short description
(
edit
)
Template:Webarchive
(
edit
)