Becomes: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Becomes
| Old | New | Line | |
|---|---|---|---|
| 1 | + | <div style="float:right; margin: 0 10px 20px 0; padding:40px; font-size:500%; font-family: Georgia; background-color: #f9f9f9; border: 2px solid #777777;"> | |
| 2 | + | ||
| 3 | + | ||
| 4 | + | = | |
| 5 | + | ||
| 6 | + | </div> | |
| 7 | + | ||
| 8 | + | 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 | + | ||
| 10 | + | ## assignment | |
| 11 | + | ||
| 12 | + | 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 | + | ||
| 14 | + | ``` pascal | |
| 15 | + | program assignmentDemo(input, output, stderr); | |
| 16 | + | ||
| 17 | + | const | |
| 18 | + | diameter = 6; | |
| 19 | + | ||
| 20 | + | var | |
| 21 | + | n: integer; | |
| 22 | + | area: real; | |
| 23 | + | givenName: string; | |
| 24 | + | ||
| 25 | + | begin | |
| 26 | + | n := 42; | |
| 27 | + | area := pi() * diameter; | |
| 28 | + | givenName := 'Smith'; | |
| 29 | + | n := 1000 - n div 2; | |
| 30 | + | end. | |
| 31 | + | ``` | |
| 32 | + | ||
| 33 | + | 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 | + | ||
| 35 | + | ``` pascal | |
| 36 | + | program assignmentRange(input, output, stderr); | |
| 37 | + | ||
| 38 | + | type | |
| 39 | + | naturalNumber = 1..high(longword); | |
| 40 | + | ||
| 41 | + | var | |
| 42 | + | n: naturalNumber; | |
| 43 | + | ||
| 44 | + | begin | |
| 45 | + | {$rangechecks on} | |
| 46 | + | n := 1; // is OK | |
| 47 | + | n := -42 + n; // will cause RTE 201 | |
| 48 | + | end. | |
| 49 | + | ``` | |
| 50 | + | ||
| 51 | + | ## handling of special data types | |
| 52 | + | ||
| 53 | + | 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 | + | ||
| 55 | + | The compiler will generate appropriate code *copying* data for following data types. | |
| 56 | + | ||
| 57 | + | - [[String|string]] | |
| 58 | + | - [[Object|object]] | |
| 59 | + | - [[Record|record]] | |
| 60 | + | ||
| 61 | + | You don’t have to iterate over all components copying each element by hand as it is required in other programming languages. | |
| 62 | + | ||
| 63 | + | ## syntax justification | |
| 64 | + | ||
| 65 | + | 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 | + | ||
| 67 | + | In comparison other languages than Pascal allow to write | |
| 68 | + | ||
| 69 | + | ``` C | |
| 70 | + | n = m = x; | |
| 71 | + | ``` | |
| 72 | + | ||
| 73 | + | 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 | + | ||
| 75 | + | In Pascal the code excerpt above is illegal. Non-productive statements are not allowed, i. e. *something* has to be *done*. | |
| 76 | + | ||
| 77 | + | ## index specification | |
| 78 | + | ||
| 79 | + | 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 | + | ||
| 81 | + | ## see also | |
| 82 | + | ||
| 83 | + | - [[Single assignment|single assignment]], a compiler optimization | |
| 84 | + | ||
| 85 | + | --- | |
| 86 | + | *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).* |
