Sign in Join
Wiki copytemporary

Declaration: what changed

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

OldNewLine
11A **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.
22
33## characteristics
44
55Declarations 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]].
66
77Nevertheless, 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.
88
99## types
1010
1111[[Pascal]] knows following types of declarations:
1212
1313- modules
1414- [[Constant|constants]]
1515- resource strings (non-standard extension)
1616- [[Type|data types]]
1717- [[Variable|variables]]
1818- [[Label|labels]] (legacy)
1919- [[Routine|routines]]
2020
2121## difference to definitions
2222
2323Declarations 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.
2424
2525- In a [[Program|program]] the program header is the declaration, the subsequent [[Block|block]] *defines* the program.
2626- The same applies for routines.
2727- Labels are declared in the `label` section
2828
2929`   systemCrash;`
3030
31-</syntaxhighlight>
32-
3331Their definition, i. e. actually associating this identifier with an address, occurs later in the source code:
3432
3533``` pascal
3634begin
3735 …
3836 if somethingIsWrong then
3937 begin
4038 goto systemCrash;
4139 end;
4240 …
4341systemCrash:
4442 …
4543```
4644
4745- Constants are declared and defined in one brush: In
4846
4947`   answer = 42;`
5048
51-</syntaxhighlight>
52-
5349the information “`answer` is an integer constant” is the declaration. The information “`answer` equals `42`” is the definition. The same applies to resource strings.
5450
5551- Data types are frequently defined implicitly. The following only *declares* a data type `point`:
5652
5753`   point = record`\
5854`           x, y: integer;`\
5955`       end;`
60-
61-</syntaxhighlight>
6256
6357The 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]].
6458
6559- 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]].
6660
6761In summary, breach of agreed *declarations* cause [[compile-time error|compile-time errors]], whereas *wrong definitions* cannot be caught by the compiler.
6862
6963---
7064*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).*
7165Category: Glossary

Back to history