A declaration introduces the compiler to new identifiers. It is an agreement between the programmer and compiler, that a certain symbol henceforth has a fixed meaning.
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 compiled binary file.
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.
Pascal knows following types of declarations:
- resource strings (non-standard extension)
- data types
- labels (legacy)
difference to definitions
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.
- In a
programthe program header is the declaration, the subsequent block defines the program.
- The same applies for routines.
- Labels are declared in the
labelsectionTheir definition, i. e. actually associating this identifier with an address, occurs later in the source code:
begin … if somethingIsWrong then begin goto systemCrash; end; … systemCrash: …
- Constants are declared and defined in one brush: Inthe information “
const answer = 42;
answeris a constant” is the declaration. The information “
42” is the definition. The same applies to resource strings.
- Data types are frequently defined implicitly. The following only declares a data type
point:The definition of
type point = record x, y: integer; end;
pointis invisible: Which operations are allowed on
pointis not written in this piece of code. Nevertheless, the assignment of a
pointvalue to a
pointvariable works out of the box, and further operations on
pointcan be defined using operator overloading.
- 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 assignment.
In summary, breach of agreed declarations cause compile-time errors, whereas wrong definitions cannot be caught by the compiler.