Sign in Join
Wiki copytemporary

Declaration: what changed

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

Title: Declaration

OldNewLine
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+
3+## characteristics
4+
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+
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+
9+## types
10+
11+[[Pascal]] knows following types of declarations:
12+
13+- modules
14+- [[Constant|constants]]
15+- resource strings (non-standard extension)
16+- [[Type|data types]]
17+- [[Variable|variables]]
18+- [[Label|labels]] (legacy)
19+- [[Routine|routines]]
20+
21+## difference to definitions
22+
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+
25+- In a [[Program|program]] the program header is the declaration, the subsequent [[Block|block]] *defines* the program.
26+- The same applies for routines.
27+- Labels are declared in the `label` section
28+
29+`   systemCrash;`
30+
31+</syntaxhighlight>
32+
33+Their definition, i. e. actually associating this identifier with an address, occurs later in the source code:
34+
35+``` pascal
36+begin
37+ …
38+ if somethingIsWrong then
39+ begin
40+ goto systemCrash;
41+ end;
42+ …
43+systemCrash:
44+ …
45+```
46+
47+- Constants are declared and defined in one brush: In
48+
49+`   answer = 42;`
50+
51+</syntaxhighlight>
52+
53+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+
55+- Data types are frequently defined implicitly. The following only *declares* a data type `point`:
56+
57+`   point = record`\
58+`           x, y: integer;`\
59+`       end;`
60+
61+</syntaxhighlight>
62+
63+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+
65+- 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+
67+In summary, breach of agreed *declarations* cause [[compile-time error|compile-time errors]], whereas *wrong definitions* cannot be caught by the compiler.
68+
69+---
70+*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+Category: Glossary

Back to history