Sign in Join
Wiki copytemporary

Becomes: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
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-
41=
5-
6-</div>
72
83The 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.\
94
105## assignment
116
127For 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.
138
149``` pascal
1510program assignmentDemo(input, output, stderr);
1611
1712const
1813 diameter = 6;
1914
2015var
2116 n: integer;
2217 area: real;
2318 givenName: string;
2419
2520begin
2621 n := 42;
2722 area := pi() * diameter;
2823 givenName := 'Smith';
2924 n := 1000 - n div 2;
3025end.
3126```
3227
3328Assignments 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.
3429
3530``` pascal
3631program assignmentRange(input, output, stderr);
3732
3833type
3934 naturalNumber = 1..high(longword);
4035
4136var
4237 n: naturalNumber;
4338
4439begin
4540 {$rangechecks on}
4641 n := 1; // is OK
4742 n := -42 + n; // will cause RTE 201
4843end.
4944```
5045
5146## handling of special data types
5247
5348Where 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.
5449
5550The compiler will generate appropriate code *copying* data for following data types.
5651
5752- [[String|string]]
5853- [[Object|object]]
5954- [[Record|record]]
6055
6156You don’t have to iterate over all components copying each element by hand as it is required in other programming languages.
6257
6358## syntax justification
6459
6560The 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.
6661
6762In comparison other languages than Pascal allow to write
6863
6964``` C
7065n = m = x;
7166```
7267
7368The 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.
7469
7570In Pascal the code excerpt above is illegal. Non-productive statements are not allowed, i. e. *something* has to be *done*.
7671
7772## index specification
7873
7974In 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.
8075
8176## see also
8277
8378- [[Single assignment|single assignment]], a compiler optimization
8479
8580---
8681*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).*

Back to history