Variable: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Variable
| Old | New | Line | |
|---|---|---|---|
| 1 | + | A **variable** is an [[Identifier|identifier]] associated with a chunk of memory that can be inspected and manipulated during [[runtime|run-time]] in accordance with an associated [[Data type|data type]]. | |
| 2 | + | ||
| 3 | + | ## declaration | |
| 4 | + | ||
| 5 | + | Variables are [[Declaration|declared]] in a [[Var|var section]]. In [[Pascal]] every variable has a data type already known at [[Compile time|compile-time]] (and let it be the [[Variant|data type variant]]). A variable is declared by a $`(\text{identifier}, \text{data type identifier})`$ tuple, separated by a [[Colon|colon]]: | |
| 6 | + | ||
| 7 | + | ``` pascal | |
| 8 | + | var | |
| 9 | + | foo: char; | |
| 10 | + | ``` | |
| 11 | + | ||
| 12 | + | According to the data type’s space requirements, the appropriate amount of memory is reserved on the stack, as soon as the corresponding [[Scope|scope]] is entered. Depending on where the `var`-section is placed, you can speak of either [[Global variables|global]] or [[Local variables|local]] variables. | |
| 13 | + | ||
| 14 | + | ## manipulation | |
| 15 | + | ||
| 16 | + | Variables are manipulated by the [[Becomes|assignment operator :=]]. Furthermore a series of built-in procedures implicitly assign values to a variable: | |
| 17 | + | ||
| 18 | + | - input/output routines like, [[Get|get]] and [[Put|put]], [[Read|read]] and `readLn`, [[Assign|assign]] and `close` | |
| 19 | + | - `new` and `dispose` when handling [[Pointer|pointers]] to [[Object|objects]] | |
| 20 | + | - `setLength` when handling [[Dynamic array|dynamic arrays]] | |
| 21 | + | ||
| 22 | + | ## definition | |
| 23 | + | ||
| 24 | + | In [[Extended Pascal]] a variable can be defined, that means declared and initialized, in one term by doing the following. | |
| 25 | + | ||
| 26 | + | ``` pascal | |
| 27 | + | var | |
| 28 | + | x: integer value 42; | |
| 29 | + | ``` | |
| 30 | + | ||
| 31 | + | The [[FPC]] as of version 3.2.0 does only support Borland Delphi’s `=` notation: | |
| 32 | + | ||
| 33 | + | ``` pascal | |
| 34 | + | var | |
| 35 | + | x: integer = 42; | |
| 36 | + | ``` | |
| 37 | + | ||
| 38 | + | The VAX Pascal notation using `:=` is not supported at all. | |
| 39 | + | ||
| 40 | + | ## access | |
| 41 | + | ||
| 42 | + | A variable is accessed, that means the value at the referenced memory position is read, by simply specifying its identifier (wherever an [[expression]] is expected). | |
| 43 | + | ||
| 44 | + | Note, there are a couple data types which are in fact pointers, but are automatically de-referenced, including but not limited to [[Class|classes]], dynamic arrays and [[Ansistring|ANSI strings]]. With `{$modeSwitch autoDeref+}` (not recommended) also typed pointers are silently de-referenced without the [[^|^ (hat symbol)]] being present. This means, you do not necessarily operate on the actual memory block the variable is genuinely associated with, but somewhere else. | |
| 45 | + | ||
| 46 | + | Usually the variable’s memory chunk is interpreted according to its data type as it was declared as. With [[Typecast|typecasts]] the interpretation of a given variable’s memory block can be altered (per expression). | |
| 47 | + | ||
| 48 | + | ## memory alias | |
| 49 | + | ||
| 50 | + | In conjunction with the [[Keyword|keyword]] [[Absolute|absolute]] an identifier can be associated with a previously reserved blob of memory. While a plain $`(\text{identifier}, \text{data type})`$ tuple actually sets a certain amount of memory aside, the following declaration of `c` does not occupy any additional space, but links the identifier `c` with the memory block that has been reserved for `x`: | |
| 51 | + | ||
| 52 | + | ``` pascal | |
| 53 | + | var | |
| 54 | + | x: byte; | |
| 55 | + | c: char absolute x; | |
| 56 | + | ``` | |
| 57 | + | ||
| 58 | + | Here, the memory alias was used as a, one of many, strategies to convince the [[Compiler|compiler]] to allow operations valid for the [[Char|char type]] while the underlying memory was originally reserved for a [[Byte|byte]]. This feature has to be chosen wisely. It necessarily requires knowledge of data type’s memory structure, if nothing is supposed to trigger any sort of access violations. | |
| 59 | + | ||
| 60 | + | Most importantly, the additionally referenced memory will be treated as if it was declared regularly. No questions asked. | |
| 61 | + | ||
| 62 | + | ## see also | |
| 63 | + | ||
| 64 | + | - [[Constant|constant]] | |
| 65 | + | ||
| 66 | + | --- | |
| 67 | + | *From the Free Pascal wiki, page "Variable", as it stood on 2022-09-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).* |
