Sign in Join
Wiki copytemporary

Unit: what changed

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

OldNewLine
1-A `unit` is a [[Source code|source code]] file (or the [[Binary|binary]] compiled from that [[File|file]]) which was written using the [[Pascal]] programming language, and that is designed to be a single module in an [[Application|application]] or an [[Object module|object module]]. `Unit` is a [[Reserved word|reserved word]]. It must appear before anything in the unit except comments.
1+A `unit` is a [[Source code|source code]] file (or the [[Binary|binary]] compiled from that [[File|file]]) which was written using the [[Pascal]] programming language, and that is designed to be a single module in an [[Application|application]] or an [[Object module|object module]]. `Unit` is a [[Reserved word|reserved word]]. It must appear before anything in the unit except comments.
22
33## Purpose
44
5-A unit may be used where functionality needs to be provided to an application program or to other units. It allows writing code that performs that functionality once and have it used in many places. This can reduce the possibility of errors and increase code reusage.
5+A unit may be used where functionality needs to be provided to an application program or to other units. It allows writing code that performs that functionality once and have it used in many places. This can reduce the possibility of errors and increase code reusage.
66
7-A binary unit may be used where a unit author wishes to provide certain functionality to be used in a Pascal [[Program|program]] but does not wish to provide the source code that performs that functionality.
7+A binary unit may be used where a unit author wishes to provide certain functionality to be used in a Pascal [[Program|program]] but does not wish to provide the source code that performs that functionality.
88
9-Units were also used on older versions of Pascal when it was necessary on computers with limited resources to be able to load [[Routine|routines]] as needed rather than keeping every routine of the [[Executable program|executable program]] in memory all of the time.
9+Units were also used on older versions of Pascal when it was necessary on computers with limited resources to be able to load [[Routine|routines]] as needed rather than keeping every routine of the [[Executable program|executable program]] in memory all of the time.
1010
11-A unit or program that needs to access the [[Procedure|procedures]] and [[Data type|data types]] in another unit must specify the units it needs to access them in a [[Uses|uses statement]] (but linking is done without the need to write a makefile as in C).
11+A unit or program that needs to access the [[Procedure|procedures]] and [[Data type|data types]] in another unit must specify the units it needs to access them in a [[Uses|`uses` statement]] (but linking is done without the need to write a makefile as in C).
1212
13-A unit may also be used to declare a series of global [[Const|constants]] or [[Global variables|variables]] for use by the entire application, without actually containing any executable code. This is similar to the `common` [[Keyword|keyword]] in the [[Fortran]] programming language.
13+A unit may also be used to declare a series of global [[Const|constants]] or [[Global variables|variables]] for use by the entire application, without actually containing any executable code. This is similar to the `common` [[Keyword|keyword]] in the [[Fortran]] programming language.
1414
1515## Format
1616
17-A unit is defined with the `unit` keyword followed by the unit-identifier. The unit-identifier (in the following example the unit's name is “minimalunit”) should match the filename it is written in. A minimal working example (which does nothing) is:
18-
19-``` pascal
17+A unit is defined with the `unit` keyword followed by the unit-identifier. The unit-identifier (in the following example the unit's name is “minimalunit”) should match the filename it is written in. A minimal working example (which does nothing) is:
18+```
2019unit minimalunit;
21-interface
22- // here comes stuff that the unit publicly offers
23-implementation
24- // here comes the implementation of offered stuff and
25- // optionally internal stuff (only known in the unit)
26-end.
20+ interface
21+ // here comes stuff that the unit publicly offers
22+ implementation
23+ // here comes the implementation of offered stuff and
24+ // optionally internal stuff (only known in the unit)
25+ end.
2726```
2827
29-where the part after [[Interface|interface corresponds]] to the `public`-part of other languages and [[Implementation|implementation]] does so to `private`.
30-
31-A more advanced basic structure is:
28+where the part after [[Interface|`interface` corresponds]] to the `public`-part of other languages and [[Implementation|`implementation`]] does so to `private`.
3229
33-``` pascal
30+A more advanced basic structure is:
31+```
3432unit advancedunit;
35-interface
36-
37-implementation
38-
39-initialization
40- // here may be placed code that is
41- // executed as the unit gets loaded
42-
43-finalization
44- // code executed at program end
45-
46-end.
33+ interface
34+
35+ implementation
36+
37+ initialization
38+ // here may be placed code that is
39+ // executed as the unit gets loaded
40+
41+ finalization
42+ // code executed at program end
43+
44+ end.
4745```
4846
49-The optional [[Initialization|initialization]] and [[Finalization|finalization]] blocks may be followed by code that is executed on program start/end.
47+The optional [[Initialization|`initialization`]] and [[Finalization|`finalization`]] blocks may be followed by code that is executed on program start/end.
5048
5149### Detailed unit example
5250
53-A step-by-step example:
54-
55-``` pascal
51+A step-by-step example:
52+```
5653unit randomunit;
57-// this unit does something
58-
59-// public - - - - - - - - - - - - - - - - - - - - - - - - -
60-interface
61-
62-type
63- // the type TRandomNumber gets globally known
64- // since it is included somewhere (uses-statement)
65- TRandomNumber = integer;
66-
67-// of course the const- and var-sections are possible, too
68-
69-// a list of procedure/function signatures makes
70-// them usable from outside of the unit
71-function getRandomNumber(): TRandomNumber;
72-
73-// an implementation of a function/procedure
74-// must not be in the interface-part
75-
76-// private - - - - - - - - - - - - - - - - - - - - - - - - -
77-implementation
78-
79-var
80- // var in private-part
81- // => only modifiable inside from this unit
82- chosenRandomNumber: TRandomNumber;
83-
84-function getRandomNumber(): TRandomNumber;
85-begin
86- // return value
87- getRandomNumber := chosenRandomNumber;
88-end;
89-
90-// initialization is the part executed
91-// when the unit is loaded/included
92-initialization
93-begin
94- // choose our random number
95- chosenRandomNumber := 3;
96- // chosen by fair-dice-roll
97- // guaranteed to be random
98-end;
99-
100-// finalization is executed at program end
101-finalization
102-begin
103- // this unit says 'bye' at program halt
104- writeln('bye');
105-end;
106-
107-// initialization and finalization
108-// are executed at most _once_
109-// during the entire runtime of a program
110-end.
54+ // this unit does something
55+
56+ // public - - - - - - - - - - - - - - - - - - - - - - - - -
57+ interface
58+
59+ type
60+ // the type TRandomNumber gets globally known
61+ // since it is included somewhere (uses-statement)
62+ TRandomNumber = integer;
63+
64+ // of course the const- and var-sections are possible, too
65+
66+ // a list of procedure/function signatures makes
67+ // them usable from outside of the unit
68+ function getRandomNumber(): TRandomNumber;
69+
70+ // an implementation of a function/procedure
71+ // must not be in the interface-part
72+
73+ // private - - - - - - - - - - - - - - - - - - - - - - - - -
74+ implementation
75+
76+ var
77+ // var in private-part
78+ // => only modifiable inside from this unit
79+ chosenRandomNumber: TRandomNumber;
80+
81+ function getRandomNumber(): TRandomNumber;
82+ begin
83+ // return value
84+ getRandomNumber := chosenRandomNumber;
85+ end;
86+
87+ // initialization is the part executed
88+ // when the unit is loaded/included
89+ initialization
90+ begin
91+ // choose our random number
92+ chosenRandomNumber := 3;
93+ // chosen by fair-dice-roll
94+ // guaranteed to be random
95+ end;
96+
97+ // finalization is executed at program end
98+ finalization
99+ begin
100+ // this unit says 'bye' at program halt
101+ writeln('bye');
102+ end;
103+
104+ // initialization and finalization
105+ // are executed at most _once_
106+ // during the entire runtime of a program
107+ end.
111108```
112109
113-To include a unit just use the `uses`-statement.
114-
115-``` pascal
110+To include a unit just use the `uses`-statement.
111+```
116112program chooseNextCandidate;
117-uses
118- // include a unit
119- randomunit;
120-
121-begin
122- writeln('next candidate: no. ' + getRandomNumber());
123-end.
113+ uses
114+ // include a unit
115+ randomunit;
116+
117+ begin
118+ writeln('next candidate: no. ' + getRandomNumber());
119+ end.
124120```
125121
126-When compiling, FPC checks this program for unit dependencies. It has to be able to find the unit “randomunit”.
122+When compiling, FPC checks this program for unit dependencies. It has to be able to find the unit “randomunit”.
127123
128-The simplest way to satisfy this is to have a unit whose name matches the file name it is written in (appending `.pas` is OK and encouraged). The unit file may be in the same directory where the program source is in or in the unit path FPC looks for units.
124+The simplest way to satisfy this is to have a unit whose name matches the file name it is written in (appending `.pas` is OK and encouraged). The unit file may be in the same directory where the program source is in or in the unit path FPC looks for units.
129125
130126## Unit precedence
131127
132-When multiple units are described in a use clause, conflicts can occur with identifiers (procedures, types, functions etc.) that have the same name in multiple units. In FreePascal, the last unit “wins” and provides the code for the unit.
133-
134-If you want to achieve a different behavior, you can explicitly prepend *`unitname.`*`identifier` to specify the unit you want to use, or reorder the units. The former is often the clearest option.
128+When multiple units are described in a use clause, conflicts can occur with identifiers (procedures, types, functions etc.) that have the same name in multiple units. In FreePascal, **the last unit “wins”** and provides the code for the unit (an **exception** from this rule see next chapter).
135129
136-``` pascal
130+If you want to achieve a different behavior, you can explicitly prepend `_unitname._ identifier` to specify the unit you want to use, or reorder the units. The former is often the clearest option.
131+```
137132unit interestCalculations;
138-
139-interface
140-
141-type
142- basisType = currency;
143-
144-implementation
133+
134+ interface
135+
136+ type
137+ basisType = currency;
138+
139+ implementation
140+
141+ end.
142+```
145143
146-end.
144+Specifying the unit of declaration explicitly:
147145```
146+program interestCalculator;
147+
148+ uses
149+ interestCalculations;
150+
151+ type
152+ // we already loaded a declaration of "basisType"
153+ // from the interestCalculations unit, but we're
154+ // re-declaring it here again ("shadowing")
155+ basisType = extended;
156+
157+ var
158+ // last declaration wins: originalCapital is an extended
159+ originalCapital: basisType;
160+ // specify a scope to use the declaration valid there
161+ monthlyMargin: interestCalculations.basisType;
162+
163+ begin
164+ end.
165+```
148166
149-Specifying the unit of declaration explicitly:
167+Note, the `interestCalculations` unit will still perform its own calculations with its own `basisType` (here [[Currency|`currency`]]). You can only alter (“shadow”) declarations in the current [[Scope|scope]] (and descending).
150168
151-``` pascal
152-program interestCalculator;
169+### Current unit has precedence over the used units
153170
154-uses
155- interestCalculations;
171+Suppose you have a Unit 'U1' ...
156172
157-type
158- // we already loaded a declaration of "basisType"
159- // from the interestCalculations unit, but we're
160- // re-declaring it here again ("shadowing")
161- basisType = extended;
173+ * and in the **implementation** section of Unit 'U1' you write 'uses U2'
174+ * and in the interface section of Unit 'U2' e.g. a const is declared
175+ * and in the interface section of Unit 'U1' a const with the **same name** is declared
176+ * and in the implementation section of Unit 'U1' you use this const
162177
163-var
164- // last declaration wins: originalCapital is an extended
165- originalCapital: basisType;
166- // specify a scope to use the declaration valid there
167- monthlyMargin: interestCalculations.basisType;
178+In this case the const from Unit 'U1' has **precedence** over the const from Unit 'U2' because the U1 unit is searched first.
168179
169-begin
170-end.
180+Skeleton of this example:
171181```
172-
173-Note, the `interestCalculations` unit will still perform its own calculations with its own `basisType` (here [[Currency|currency]]). You can only alter (“shadow”) declarations in the current [[Scope|scope]] (and descending).
182+unit U1;
183+
184+ interface
185+
186+ const Elements = 5;
187+
188+ implementation
189+
190+ uses U2; {has 'const Elements = 6'}
191+
192+ procedure test;
193+ begin
194+ writeln('Elements=', Elements); {output is '5'}
195+ end;
196+```
197+```
198+unit U2;
199+
200+ interface
201+
202+ const Elements = 6;
203+```
174204
175205## See also
176206
177-- [“Unit scope” in the “reference guide”](https://www.freepascal.org/docs-html/ref/refsu102.html) (FPC HTML documentation)
178-- [“Compiling a unit” in the “users's guide”](https://www.freepascal.org/docs-html/user/userse11.html) (FPC HTML documentation)
179-- [§ “Units” in the WikiBook “Pascal Programming”](https://en.wikibooks.org/wiki/Pascal_Programming/Units)
180-- [[Unit not found - How to find units]]
207+ * [“Unit scope” in the “reference guide”](<https://www.freepascal.org/docs-html/ref/refsu102.html>) (FPC HTML documentation)
208+ * [“Compiling a unit” in the “users's guide”](<https://www.freepascal.org/docs-html/user/userse11.html>) (FPC HTML documentation)
209+ * [§ “Units” in the WikiBook “Pascal Programming”](<https://en.wikibooks.org/wiki/Pascal_Programming/Units>)
210+ * [[Unit not found - How to find units]]
181211
182212---
183-*From the Free Pascal wiki, page "Unit", as it stood on 2022-01-02 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).*
213+*From the Free Pascal wiki, page "Unit", as the Internet Archive kept it on 2026-01-27, last changed on 2025-11-18. 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).*

Back to history