Jump to content

Assignment (computer science): Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
→‎Notation: moved Magik's << to the atypical table
Line 17: Line 17:
|-
|-
| <code>''variable'' := ''expression''</code> || ''[[ALGOL]], [[Pascal (programming language)|Pascal]]<ref name=pascal>{{cite book|first=Lawrie|last=Moore|authorlink=|coauthors= |year=1980|title=Foundations of Programming with Pascal|location=New York |publisher=John Wiley & Sons|isbn=0-470-26939-1}}</ref>, [[Ada (programming language)|Ada]], [[Dylan (programming language)|Dylan]]<ref name=dylan>{{cite book|first=Neal|last=Feinberg|authorlink=|coauthors=Keene, Sonya E.; Mathews, Robert O.; Withington, P. Tucker |year=1997|title=Dylan Programming|location=Massachusetts |publisher=Addison Wesley|isbn=0-201-47976-1}}</ref>, [[Eiffel (programming language)|Eiffel]]<ref name=eiffel1>{{cite book|first=Bertrand|last=Meyer|authorlink=|coauthors=|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|first=Richard|last=Wiener|authorlink=|coauthors=|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>, …''
| <code>''variable'' := ''expression''</code> || ''[[ALGOL]], [[Pascal (programming language)|Pascal]]<ref name=pascal>{{cite book|first=Lawrie|last=Moore|authorlink=|coauthors= |year=1980|title=Foundations of Programming with Pascal|location=New York |publisher=John Wiley & Sons|isbn=0-470-26939-1}}</ref>, [[Ada (programming language)|Ada]], [[Dylan (programming language)|Dylan]]<ref name=dylan>{{cite book|first=Neal|last=Feinberg|authorlink=|coauthors=Keene, Sonya E.; Mathews, Robert O.; Withington, P. Tucker |year=1997|title=Dylan Programming|location=Massachusetts |publisher=Addison Wesley|isbn=0-201-47976-1}}</ref>, [[Eiffel (programming language)|Eiffel]]<ref name=eiffel1>{{cite book|first=Bertrand|last=Meyer|authorlink=|coauthors=|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|first=Richard|last=Wiener|authorlink=|coauthors=|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>, …''
|-
| <code>''variable'' << ''expression''</code> || ''[[Magik (programming language)|Magik]]''

|-
|-
|}
|}
Line 26: Line 23:


:{| class="wikitable"
:{| class="wikitable"
|-
| <code>''variable'' << ''expression''</code> || ''[[Magik (programming language)|Magik]]''
|-
|-
| <code>''variable'' <- ''expression''</code> || ''[[Objective Caml]], [[S (programming language)|S]], [[R (programming language)|R]]'', ...
| <code>''variable'' <- ''expression''</code> || ''[[Objective Caml]], [[S (programming language)|S]], [[R (programming language)|R]]'', ...

Revision as of 13:27, 9 September 2010

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.

Basically, this means that you are taking a value, like "Bauer and West" and placing that value inside a spot in memory (a variable).
For example, in Actionscript, this will be
teachersThatRock = "Bauer and West";
Where you are taking the string, "Bauer and West" and placing it in the teachersThatRock variable.


The assignment statement often allows that the same variable name to contain different values at different times during program execution.

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 = expression BASIC, Fortran, C, Java, PL/I, Windows PowerShell, Bourne shell, …
variable := expression ALGOL, Pascal[1], Ada, Dylan[2], Eiffel[3][4], …

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

variable << expression Magik
variable <- expression Objective Caml, S, R, ...
variableexpression APL[5]
variable =: expression J
LET variable = expression BASIC
set variable to expression AppleScript
set variable = expression C shell
Set-Variable variable (expression) Windows PowerShell
val variable = expression ML[6]
variable : expression Macsyma, Maxima

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

Some expression-oriented languages, such as Lisp[7][8] and Tcl, uniformly use prefix syntax for all statements, including assignment.

(setq variable expression) Lisp, Scheme[9][10][11] (set!), …
set variable expression Tcl

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

Parallel assignment

Some programming languages, such as Occam2[12] , Python[13], Perl[14] , REBOL, Ruby[15], 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'.[16]

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[17], 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.

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.

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[which?] language processors can detect such situations, and warn the programmer of the potential error.

Notes

  1. ^ Moore, Lawrie (1980). Foundations of Programming with Pascal. New York: John Wiley & Sons. ISBN 0-470-26939-1. {{cite book}}: Cite has empty unknown parameter: |coauthors= (help)
  2. ^ Feinberg, Neal (1997). Dylan Programming. Massachusetts: Addison Wesley. ISBN 0-201-47976-1. {{cite book}}: Unknown parameter |coauthors= ignored (|author= suggested) (help)
  3. ^ Meyer, Bertrand (1992). Eiffel the Language. Hemel Hempstead: Prentice Hall International(UK). ISBN 0-13-247925-7. {{cite book}}: Cite has empty unknown parameter: |coauthors= (help)
  4. ^ Wiener, Richard (1996). An Object-Oriented Introduction to Computer Science Using Eiffel. Upper Saddle River, New Jersey: Prentice Hall. ISBN 0-13-183872-5. {{cite book}}: Cite has empty unknown parameter: |coauthors= (help)
  5. ^ Iverson, Kenneth E. (1962). A Programming Language. John Wiley and Sons. ISBN 0471430145.
  6. ^ Ullman, Jeffrey D. (1998). Elements of ML Programming: ML97 Edition. Englewood Cliffs, New Jersey: Prentice Hall. ISBN 0-13-790387-1. {{cite book}}: Cite has empty unknown parameter: |coauthors= (help)
  7. ^ Graham, Paul (1996). Ansi Common Lisp. New Jersey: Prentice Hall. ISBN 0-13-370875-6. {{cite book}}: Cite has empty unknown parameter: |coauthors= (help)
  8. ^ Steele, Guy L. (1990). Common Lisp - The Language. Lexington: Digital Press. ISBN 1-55558-041-6. {{cite book}}: Cite has empty unknown parameter: |coauthors= (help)
  9. ^ Dybvig, R. Kent (1996). The Scheme Programming Language: Ansi Scheme. New Jersey: Prentice Hall. ISBN 0-13-454646-6. {{cite book}}: Cite has empty unknown parameter: |coauthors= (help)
  10. ^ Smith, Jerry D. (1988). Introduction to Scheme. New Jersey: Prentice Hall. ISBN 0-13-496712-7. {{cite book}}: Cite has empty unknown parameter: |coauthors= (help)
  11. ^ Abelson, Harold (1996). Structure and Interpretation of Computer Programs. New Jersey: McGraw-Hill. ISBN 0-07-000484-6. {{cite book}}: Unknown parameter |coauthors= ignored (|author= suggested) (help)
  12. ^ INMOS Limited, ed. (1988). Occam 2 Reference Manual. New Jersey: Prentice Hall. ISBN 0-13-629312-3. {{cite book}}: Cite has empty unknown parameter: |coauthors= (help)
  13. ^ Lutz, Mark (2001). Python programming language (2 ed.). Sebastopol: O´Reilly. ISBN 0-596-00085-5. {{cite book}}: Cite has empty unknown parameter: |coauthors= (help)
  14. ^ Wall, Larry (1996). Perl programming language (2 ed.). Cambridge: O´Reilly. ISBN 1-56592-149-6. {{cite book}}: Unknown parameter |coauthors= ignored (|author= suggested) (help)
  15. ^ Thomas, David (2001). Programming Ruby: The Pragmatic Programmer´s Guide. Upper Saddle River: Addison Wesley. ISBN 0-201-71089-7. {{cite book}}: Unknown parameter |coauthors= ignored (|author= suggested) (help)
  16. ^ D.W. Barron et al., "The main features of CPL", Computer Journal 6:2:140 (1963). full text (subscription)
  17. ^ Hudak, Paul (2000). The Haskell School of Expression: Learning Functional Programming Through Multimedia. Cambridge: Cambridge University Press. ISBN 0-521-64408-9. {{cite book}}: Cite has empty unknown parameter: |coauthors= (help)

See also

Template:Link GA