Becomes: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | - | ||
| 2 | - | ||
| 3 | - | ||
| 4 | 1 | = | |
| 5 | - | ||
| 6 | - | ||
| 7 | 2 | ||
| 8 | 3 | The character pair `:=`, that are a [[Colon|colon]] and an [[Equal|equal sign]] back to back, is pronounced as “becomes” and used by [[Pascal]] as the assignment operator.\ | |
| 9 | 4 | ||
| 10 | 5 | ## assignment | |
| 11 | 6 | ||
| 12 | 7 | For valid assignments `:=` is surrounded by a *single* [[Variable|variable]] [[Identifier|identifier]] on the left hand (possibly doing a [[Typecast|variable typecast]]), and an [[expression]] evaluating to the [[Data type|data type]] the variable is declared as on the right hand. | |
| 13 | 8 | ||
| 14 | 9 | ``` pascal | |
| 15 | 10 | program assignmentDemo(input, output, stderr); | |
| 16 | 11 | ||
| 17 | 12 | const | |
| 18 | 13 | diameter = 6; | |
| 19 | 14 | ||
| 20 | 15 | var | |
| 21 | 16 | n: integer; | |
| 22 | 17 | area: real; | |
| 23 | 18 | givenName: string; | |
| 24 | 19 | ||
| 25 | 20 | begin | |
| 26 | 21 | n := 42; | |
| 27 | 22 | area := pi() * diameter; | |
| 28 | 23 | givenName := 'Smith'; | |
| 29 | 24 | n := 1000 - n div 2; | |
| 30 | 25 | end. | |
| 31 | 26 | ``` | |
| 32 | 27 | ||
| 33 | 28 | Assignments to variables of a [[subrange types|subrange type]] should be handled with care. The [[Compiler|compiler]] can only yield out-of-range errors for *[[Constant|constant]]* expressions, i. e. not depending on any [[runtime|run-time]] data. With [[sRangechecks|{$rangeChecks}]] enabled a [[runtime error|run-time error]] can be generated. | |
| 34 | 29 | ||
| 35 | 30 | ``` pascal | |
| 36 | 31 | program assignmentRange(input, output, stderr); | |
| 37 | 32 | ||
| 38 | 33 | type | |
| 39 | 34 | naturalNumber = 1..high(longword); | |
| 40 | 35 | ||
| 41 | 36 | var | |
| 42 | 37 | n: naturalNumber; | |
| 43 | 38 | ||
| 44 | 39 | begin | |
| 45 | 40 | {$rangechecks on} | |
| 46 | 41 | n := 1; // is OK | |
| 47 | 42 | n := -42 + n; // will cause RTE 201 | |
| 48 | 43 | end. | |
| 49 | 44 | ``` | |
| 50 | 45 | ||
| 51 | 46 | ## handling of special data types | |
| 52 | 47 | ||
| 53 | 48 | Where simple data types like [[Integer|integers]] and [[Char|characters]] are realized as `mov` instructions or alike, data types that require initialization and finalization such as [[Ansistring|ansistrings]] or [[Class|classes]] need special care. | |
| 54 | 49 | ||
| 55 | 50 | The compiler will generate appropriate code *copying* data for following data types. | |
| 56 | 51 | ||
| 57 | 52 | - [[String|string]] | |
| 58 | 53 | - [[Object|object]] | |
| 59 | 54 | - [[Record|record]] | |
| 60 | 55 | ||
| 61 | 56 | You don’t have to iterate over all components copying each element by hand as it is required in other programming languages. | |
| 62 | 57 | ||
| 63 | 58 | ## syntax justification | |
| 64 | 59 | ||
| 65 | 60 | The rationale of using two characters for assignment instead of just one, say the `=` sign, is to distinguish between assigning values and comparing for equality. It’s got its roots in the field of mathematics where a single equal sign is read as an expression, but has no imperative connotation. | |
| 66 | 61 | ||
| 67 | 62 | In comparison other languages than Pascal allow to write | |
| 68 | 63 | ||
| 69 | 64 | ``` C | |
| 70 | 65 | n = m = x; | |
| 71 | 66 | ``` | |
| 72 | 67 | ||
| 73 | 68 | The semantics of this line of code varies among every programming language. For instance, in [[Fortran]] and in Basic this line means “Compare the values `m` and `x`, and if they are equal to each other `n` becomes `true`, `false` otherwise.” In contrast to that the C programming language will assign `m` the value of `x`, and subsequently assign the `n` the value of `m`, so `n` and `m` both have the value `x`. This sort of code has been a common source of errors, compilers nowadays even emit warnings when encountering multiple assignment in a single line. | |
| 74 | 69 | ||
| 75 | 70 | In Pascal the code excerpt above is illegal. Non-productive statements are not allowed, i. e. *something* has to be *done*. | |
| 76 | 71 | ||
| 77 | 72 | ## index specification | |
| 78 | 73 | ||
| 79 | 74 | In declarations of enumerated data types, `:=` can be used to explicitly specify the ordinal value of a member. See [[Enum Type|enumeration types § “Indices”]] for details. | |
| 80 | 75 | ||
| 81 | 76 | ## see also | |
| 82 | 77 | ||
| 83 | 78 | - [[Single assignment|single assignment]], a compiler optimization | |
| 84 | 79 | ||
| 85 | 80 | --- | |
| 86 | 81 | *From the Free Pascal wiki, page "Becomes", as it stood on 2021-02-03 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
