Difference between revisions of "Unit"

From Free Pascal wiki
Jump to navigationJump to search
Line 3: Line 3:
 
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]].   
 
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]].   
  
A unit may be used where certain functionality is to be provided to an application program or to other units, and allowing code that performs that functionality to be created once and used in many places. This can reduce the possibility of error and increase the possibility of code reuse.
+
== Aim ==
 +
A unit may be used where certain functionality is to be provided to an application program or to other units, and allowing code that performs that functionality to be created once and used in many places.
 +
This can reduce the possibility of errors and increase the chance of code reusage.
  
A binary unit may be used where a program author wishes to provide certain functionality for use in a Pascal program but does not wish to provide the source code that performs that functionality.
+
A binary unit may be used where a unit author wishes to provide certain functionality to be used in a Pascal program but does not wish to provide the source code that performs that functionality.
  
 
Units were also used on older versions of Pascal when it was nececessary 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.
 
Units were also used on older versions of Pascal when it was nececessary 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.
 +
A unit that needs to access e. g. [[Procedure|procedures]] and [[Data type|data types]] in another unit must specify those units it needs to access in a [[Uses]] statement but linking is done without the need to write a makefile as in C.
  
A unit that needs to access e.g. [[Procedure|procedures]] and [[Data type|data types]] in another unit must specify those units it needs to access in a [[Uses]] statement but linking is done without the need to write a makefile as in C.
+
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 in the Fortran Programming Language.
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 in the Fortran Programming Language.
 
  
 
== Format ==
 
== Format ==
Line 28: Line 30:
  
 
A more advanced basic structure is
 
A more advanced basic structure is
 +
<syntaxhighlight>
 +
unit advancedunit;
 +
interface
 +
 +
implementation
 +
 +
initialization
 +
(* here may be placed code that is *)
 +
(* executed as the unit gets loaded *)
 +
 +
finalization
 +
(* code executed at program end *)
 +
 +
end.
 +
</syntaxhighlight>
 +
 +
=== Described unit-example ===
 
<syntaxhighlight>
 
<syntaxhighlight>
 
unit randomunit;
 
unit randomunit;

Revision as of 03:18, 20 January 2014

Deutsch (de) English (en) español (es) suomi (fi) français (fr) português (pt) русский (ru)

A unit is a source code file (or the binary compiled from that file) which was written using the Pascal programming language, and that is designed to be a single module in an application or an object module.

Aim

A unit may be used where certain functionality is to be provided to an application program or to other units, and allowing code that performs that functionality to be created once and used in many places. This can reduce the possibility of errors and increase the chance of code reusage.

A binary unit may be used where a unit author wishes to provide certain functionality to be used in a Pascal program but does not wish to provide the source code that performs that functionality.

Units were also used on older versions of Pascal when it was nececessary on computers with limited resources to be able to load routines as needed rather than keeping every routine of the executable program in memory all of the time. A unit that needs to access e. g. procedures and data types in another unit must specify those units it needs to access in a Uses statement but linking is done without the need to write a makefile as in C.

A unit may also be used to declare a series of global constants or variables for use by the entire application, without actually containing any executable code. This is similar to the common keyword in the Fortran Programming Language.

Format

A unit is defined with the unit keyword followed by the unit-identifier. A minimal working example, but doing nothing, is

unit minimalunit;
interface
	(* here comes stuff that the unit publicly offers *)
implementation
	(* here comes the implementation of offered stuff and *)
	(* optionally internal stuff (only known in the unit) *)
end.

where the part after interface corresponds to the public-part of other languages and implementation does so to private.

A more advanced basic structure is

unit advancedunit;
interface

implementation

initialization
	(* here may be placed code that is *)
	(* executed as the unit gets loaded *)

finalization
	(* code executed at program end *)

end.

Described unit-example

unit randomunit;
(* this unit does something *)

(* public *)
interface

type
	(* the type TRandomNumber gets globally known *)
	(* since it is included somewhere (uses-statement) *)
	TRandomNumber = integer;

(* of course the const- and var-blocks are possible, too *)

(* a list of procedure/function signatures makes *)
(* them useable from outside of the unit *)
function getRandomNumber(): TRandomNumber;

(* an implementation of a function/procedure *)
(* must not be in the interface-part *)

(* private *)
implementation

var
	(* var in private-part *)
	(* => only modifiable inside from this unit *)
	chosenRandomNumber: TRandomNumber;

function getRandomNumber(): TRandomNumber;
begin
	(* return value *)
	getRandomNumber := chosenRandomNumber;
end;

(* initialization is the part executed *)
(* when the unit is loaded/included *)
initialization
begin
	(* choose our random number *)
	chosenRandomNumber := 3;
	(* chosen by fair-dice-roll *)
	(* guaranteed to be random *)
end;

(* finalization is worked off at program end *)
finalization
begin
	(* this unit says 'bye' at program halt *)
	writeln('bye');
end;
end.

An editor has declared this article to be a stub, meaning that it needs more information. Can you help out and add some? If you have some useful information, you can help the Free Pascal Wiki by clicking on the edit box on the left and expanding this page.


Read more