More Complicated Example

The next example shows a program that is divided into two files: a project file and a unit file. The project file, which you can save as greeting.dpr, looks like this:

The first line declares a program called greeting, which, once again, is a console application. The uses Unitl; clause tells the compiler that the program greeting depends on a unit called Unitl. Finally, the program calls the PrintMessage procedure, passing to it the string Hello World! The PrintMessage procedure is defined in Unitl. Here is the source code for Unitl, which must be saved in a file called Unit1.pas:

Unitl defines a procedure called PrintMessage that takes a single string as an argument and sends the string to the standard output. (In Delphi, 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 Unitl. The first declaration, under the reserved word interface, makes PrintMessage available to other modules (such as greeting) that use Unitl. The second declaration, under the reserved word implementation, actually defines PrintMessage.

You can now compile Greeting from the command line by entering dcc32 greeting to produce a Win32 executable, or dccil greeting to produce a managed .NET executable.

There is no need to include Unitl 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!

Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook

Post a comment