Quick start manual
Overview
2-5
Example programs
defined in Unit1. Here’s the source code for Unit1, which must be saved in a file
called Unit1.pas:
unit Unit1;
interface
procedure PrintMessage(msg: string);
implementation
procedure PrintMessage(msg: string);
begin
Writeln(msg);
end;
end.
Unit1 defines a procedure called PrintMessage that takes a single string as an
argument and sends the string to the standard output. (In Pascal, routines that do not
return a value are called procedures. Routines that return a value are called functions.)
Notice that PrintMessage is declared twice in Unit1. The first declaration, under the
reserved word interface, makes PrintMessage available to other modules (such as
Greeting) that use Unit1. The second declaration, under the reserved word
implementation, actually defines PrintMessage.
You can now compile Greeting from the command line by entering
DCC32 Greeting
on a Windows-based system, or
dcc Greeting
on a Linux-based system. There’s no need to include Unit1 as a command-line
argument. When the compiler processes Greeting.dpr, it automatically looks for unit
files that the Greeting program depends on. The resulting executable does the same
thing as our first example: it prints the message “Hello world!”
A native application
Our next example is an application built using the Component Library for Cross-
Platform (CLX) components in the IDE. This program uses automatically generated
form and resource files, so you won’t be able to compile it from the source code
alone. But it illustrates important features of the Delphi Language. In addition to
multiple units, the program uses classes and objects, which are discussed in
Chapter 7, “Classes and objects”.
The program includes a project file and two new unit files. First, the project file:
program Greeting; { comments are enclosed in braces }
uses
QForms,
Unit1 in ‘Unit1.pas’ {Form1},
Unit2 in ‘Unit2.pas’ {Form2};
{$R *.res} { this directive links the project's resource file }