Becomes: what changed
Revision 2 to 3. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | - | ||
| 2 | - | ||
| 3 | - | ||
| 4 | - | ||
| 5 | - | ||
| 1 | + | := | |
| 6 | 2 | ||
| 7 | - | ||
| 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. | |
| 8 | 4 | ||
| 9 | - | ||
| 10 | - | ||
| 5 | + | ## Contents | |
| 11 | 6 | ||
| 12 | - | ||
| 13 | - | ||
| 7 | + | * 1 assignment | |
| 8 | + | * 2 handling of special data types | |
| 9 | + | * 3 syntax justification | |
| 10 | + | * 4 index specification | |
| 11 | + | * 5 see also | |
| 14 | 12 | ||
| 15 | - | ||
| 16 | - | ||
| 17 | - | ||
| 18 | - | ||
| 13 | + | ## assignment | |
| 19 | 14 | ||
| 20 | - | ||
| 21 | - | ||
| 22 | - | ||
| 23 | - | ||
| 24 | - | ||
| 25 | - | ||
| 15 | + | 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. | |
| 26 | 16 | ``` | |
| 27 | - | ||
| 28 | - | ||
| 17 | + | program assignmentDemo(input, output, stderr); | |
| 18 | + | ||
| 19 | + | const | |
| 20 | + | diameter = 6; | |
| 21 | + | ||
| 22 | + | var | |
| 23 | + | n: integer; | |
| 24 | + | area: real; | |
| 25 | + | givenName: string; | |
| 26 | + | ||
| 27 | + | begin | |
| 28 | + | n := 42; | |
| 29 | + | area := pi() * diameter; | |
| 30 | + | givenName := 'Smith'; | |
| 31 | + | n := 1000 - n div 2; | |
| 32 | + | end. | |
| 33 | + | ``` | |
| 29 | 34 | ||
| 30 | - | ||
| 35 | + | 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 [`{$rangeChecks}`](</index.php?title=sRangechecks&action=edit&redlink=1> "sRangechecks \(page does not exist\)") enabled a [[runtime error|run-time error]] can be generated. | |
| 36 | + | ``` | |
| 31 | 37 | program assignmentRange(input, output, stderr); | |
| 32 | - | ||
| 33 | - | ||
| 34 | - | ||
| 35 | - | ||
| 36 | - | ||
| 37 | - | ||
| 38 | - | ||
| 39 | - | ||
| 40 | - | ||
| 41 | - | ||
| 42 | - | ||
| 43 | - | ||
| 38 | + | ||
| 39 | + | type | |
| 40 | + | naturalNumber = 1..high(longword); | |
| 41 | + | ||
| 42 | + | var | |
| 43 | + | n: naturalNumber; | |
| 44 | + | ||
| 45 | + | begin | |
| 46 | + | {$rangechecks on} | |
| 47 | + | n := 1; // is OK | |
| 48 | + | n := -42 + n; // will cause RTE 201 | |
| 49 | + | end. | |
| 44 | 50 | ``` | |
| 45 | 51 | ||
| 46 | 52 | ## handling of special data types | |
| 47 | 53 | ||
| 48 | - | ||
| 54 | + | Where simple data types like [[Integer|`integer`s]] and [[Char|`char`acters]] are realized as `mov` instructions or alike, data types that require initialization and finalization such as [[Ansistring|`ansistring`s]] or [[Class|`class`es]] need special care. | |
| 49 | 55 | ||
| 50 | - | ||
| 56 | + | The compiler will generate appropriate code _copying_ data for following data types. | |
| 51 | 57 | ||
| 52 | - | ||
| 53 | - | ||
| 54 | - | ||
| 58 | + | * [[String|`string`]] | |
| 59 | + | * [[Object|`object`]] | |
| 60 | + | * [[Record|`record`]] | |
| 55 | 61 | ||
| 56 | - | ||
| 62 | + | You don’t have to iterate over all components copying each element by hand as it is required in other programming languages. | |
| 57 | 63 | ||
| 58 | 64 | ## syntax justification | |
| 59 | 65 | ||
| 60 | - | ||
| 61 | - | ||
| 62 | - | ||
| 66 | + | 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. | |
| 63 | 67 | ||
| 64 | - | ||
| 68 | + | In comparison other languages than Pascal allow to write | |
| 69 | + | ``` | |
| 65 | 70 | n = m = x; | |
| 66 | 71 | ``` | |
| 67 | 72 | ||
| 68 | - | ||
| 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. | |
| 69 | 74 | ||
| 70 | - | ||
| 75 | + | In Pascal the code excerpt above is illegal. Non-productive statements are not allowed, i. e. _something_ has to be _done_. | |
| 71 | 76 | ||
| 72 | 77 | ## index specification | |
| 73 | 78 | ||
| 74 | - | ||
| 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. | |
| 75 | 80 | ||
| 76 | 81 | ## see also | |
| 77 | 82 | ||
| 78 | - | ||
| 83 | + | * [[Single assignment|single assignment]], a compiler optimization | |
| 79 | 84 | ||
| 85 | + | ||
| 86 | + | ||
| 87 | + | navigation bar: topic: Pascal symbols single characters | [[Plus|`+` (plus)]] • [[Minus|`-` (minus)]] • [[*|`*` (asterisk)]] • [[Slash|`/` (slash)]] | |
| 88 | + | [[Equal|`=` (equal)]] • [[Greater than|`>` (greater than)]] • [[Less than|`<` (less than)]] | |
| 89 | + | [[period|`.` (period)]] • [[Colon|`:` (colon)]] • [[;|`;` (semi colon)]] | |
| 90 | + | [[^|`^` (hat)]] • [[@|`@` (at)]] | |
| 91 | + | [[Dollar sign|`$` (dollar sign)]] • [[&|`&` (ampersand)]] • [`#` (hash)](</index.php?title=Hash&action=edit&redlink=1> "Hash \(page does not exist\)") | |
| 92 | + | [['|`'` (single quote)]] | |
| 93 | + | ---|--- | |
| 94 | + | character pairs | [[Not equal|`<>` (not equal)]] • [[Less than or equal|`<=` (less than or equal)]] • `:=` (becomes) • [[Greater than or equal|`>=` (greater than or equal)]] • [[symmetric difference|`><` (symmetric difference)]] • [[Slash|`//` (double slash)]] | |
| 95 | + | ||
| 96 | + | ||
| 97 | + | **** | |
| 98 | + | ||
| 80 | 99 | --- | |
| 81 | - | ||
| 100 | + | *From the Free Pascal wiki, page "Becomes", as the Internet Archive kept it on 2021-06-06, last changed on 2021-02-04. 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).* |
