Pascal · lesson 22 of 30 · Files and errorsSplit a program into units
A unit shows its functions in the interface and keeps the code in the implementation. fpc compiles it with the program.
22-units.pas Download 22-units.pas
program UseUnit;
{$mode objfpc}{$H+}
{ The unit is in sizes.pas in the same folder. fpc compiles it too. }
uses
Sizes;
begin
WriteLn(GiB(8001563222016):0:1, ' GiB');
WriteLn(Sizes.GiB(1073741824):0:1, ' GiB');
end.
sizes.pas Download sizes.pas
unit Sizes;
{$mode objfpc}{$H+}
interface
function GiB(Bytes: Int64): Double;
implementation
function GiB(Bytes: Int64): Double;
begin
Result := Bytes / (1024 * 1024 * 1024);
end;
end.
Terminal
$ ./22-units
7452.0 GiB
1.0 GiBExercise
Add a function MiB to the unit.