Declaration: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | A **declaration** introduces the compiler to new [[Identifier|identifiers]]. It is an agreement between the programmer and compiler, that a certain symbol henceforth has a fixed meaning. | |
| 2 | 2 | ||
| 3 | 3 | ## characteristics | |
| 4 | 4 | ||
| 5 | 5 | Declarations do not have any (explicit) impact on a compiled program: For example, a declaration of a variable informs the compiler about a new identifier associated with a data type. However, neither the variable’s name, nor the data type are stored in the [[Binary|compiled binary file]]. | |
| 6 | 6 | ||
| 7 | 7 | Nevertheless, implicitly the compiler *reserves* enough *memory* and ensures only *legal operations* are performed in compliance with the data type. Thus declarations ensure the compiler can process the source code. | |
| 8 | 8 | ||
| 9 | 9 | ## types | |
| 10 | 10 | ||
| 11 | 11 | [[Pascal]] knows following types of declarations: | |
| 12 | 12 | ||
| 13 | 13 | - modules | |
| 14 | 14 | - [[Constant|constants]] | |
| 15 | 15 | - resource strings (non-standard extension) | |
| 16 | 16 | - [[Type|data types]] | |
| 17 | 17 | - [[Variable|variables]] | |
| 18 | 18 | - [[Label|labels]] (legacy) | |
| 19 | 19 | - [[Routine|routines]] | |
| 20 | 20 | ||
| 21 | 21 | ## difference to definitions | |
| 22 | 22 | ||
| 23 | 23 | Declarations merely tell the compiler “there is something”. In contrast to that, *definitions* elaborate what “something” is. All definitions will (eventually) change the program state. Declarations do not change the program state. | |
| 24 | 24 | ||
| 25 | 25 | - In a [[Program|program]] the program header is the declaration, the subsequent [[Block|block]] *defines* the program. | |
| 26 | 26 | - The same applies for routines. | |
| 27 | 27 | - Labels are declared in the `label` section | |
| 28 | 28 | ||
| 29 | 29 | ` systemCrash;` | |
| 30 | 30 | ||
| 31 | - | ||
| 32 | - | ||
| 33 | 31 | Their definition, i. e. actually associating this identifier with an address, occurs later in the source code: | |
| 34 | 32 | ||
| 35 | 33 | ``` pascal | |
| 36 | 34 | begin | |
| 37 | 35 | … | |
| 38 | 36 | if somethingIsWrong then | |
| 39 | 37 | begin | |
| 40 | 38 | goto systemCrash; | |
| 41 | 39 | end; | |
| 42 | 40 | … | |
| 43 | 41 | systemCrash: | |
| 44 | 42 | … | |
| 45 | 43 | ``` | |
| 46 | 44 | ||
| 47 | 45 | - Constants are declared and defined in one brush: In | |
| 48 | 46 | ||
| 49 | 47 | ` answer = 42;` | |
| 50 | 48 | ||
| 51 | - | ||
| 52 | - | ||
| 53 | 49 | the information “`answer` is an integer constant” is the declaration. The information “`answer` equals `42`” is the definition. The same applies to resource strings. | |
| 54 | 50 | ||
| 55 | 51 | - Data types are frequently defined implicitly. The following only *declares* a data type `point`: | |
| 56 | 52 | ||
| 57 | 53 | ` point = record`\ | |
| 58 | 54 | ` x, y: integer;`\ | |
| 59 | 55 | ` end;` | |
| 60 | - | ||
| 61 | - | ||
| 62 | 56 | ||
| 63 | 57 | The definition of `point` is invisible: Which operations are allowed on `point` is not written in this piece of code. Nevertheless, the [[Becomes|assignment]] of a `point` value to a `point` variable works out of the box, and further operations on `point` can be defined using [[Operator overloading|operator overloading]]. | |
| 64 | 58 | ||
| 65 | 59 | - The same applies to variable declarations (except there is no possibility to define operators on anonymous data types). The *definition* of a variable is an [[Becomes|assignment]]. | |
| 66 | 60 | ||
| 67 | 61 | In summary, breach of agreed *declarations* cause [[compile-time error|compile-time errors]], whereas *wrong definitions* cannot be caught by the compiler. | |
| 68 | 62 | ||
| 69 | 63 | --- | |
| 70 | 64 | *From the Free Pascal wiki, page "Declaration", as it stood on 2021-08-12 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).* | |
| 71 | 65 | Category: Glossary |
