The structure of a Pascal program in the Delphi and Lazarus programming systems. Console application
This topic discusses the structure of a Pascal program for the most common types of applications that are well suited for learning the basics of this language. The structure of programs for Lazarus and Delphi systems is considered.
Contents
- 1. Types of programs in the Lazarus programming system
- 2. Types of programs in the Delphi programming system
- 3. The structure of the simplest application in Lazarus and Delphi systems
- 4. An example of a console application that declares arbitrary elements in the description section
- Related topics
Search other resources:
1. Types of programs in the Lazarus programming system
In the Lazarus programming system, creating a new application starts with invoking a sequence of commands
File -> New...
As a result, the system will open the “New…” window (Figure 1), in which several types of projects are proposed in the Project tab.
Figure 1. Lazarus system. The window for selecting a project, module or element to be created. Project tab
The following main types of projects are proposed:
- Application – a project that supports the Windows graphical interface using cross-platform and LCL (Lazarus Component Library). It is analogous to the Windows Forms template in other programming systems;
- Simple Program – a simple template for creating simple programs in Pascal. Programs are in the form of a command line;
- Program – program template in the form of a command line with some additional options;
- Console Application – a Console Application-type program template based on the use of the TMyApplication class with the possibility of its extension;
- Library – dynamically loaded library, which is located in files with the *.dll extension (Dynamic Link Library). Such a library can be added to other programs (*.exe files) and cannot be run autonomously;
- other projects.
⇑
2. Types of programs in the Delphi programming system
To create a program (project) in the Delphi programming system, you need to select a sequence of commands
File -> New -> Others...
As a result, the window shown in Figure 2 will open.
Рисунок 2. Types of projects in the Delphi programming system
The following main types of projects (programs) are offered here:
- Console Application is a console application that has a classic program structure. It is analogous to the Program or Simple Program pattern in the Lazarus system. This kind of application is best for learning the basics of Pascal programming;
- VCL Forms Application – a kind of application that supports the Windows interface. This kind of application is based on the use of the VCL (Visual Component Library);
- Dynamik Link Library is a kind of application that is attached to another program as a *.dll file. Libraries of functions that are used in other programs are formed in *.dll files;
- other types of applications.
⇑
3. The structure of the simplest application in Lazarus and Delphi systems
The best application for learning the basics of programming in Pascal is an application like:
- Simple Program in the Lazarus programming system;
- Console Application in the Delphi programming system.
This kind of application works in Windows command line mode. For both systems, the structure of a simple application is the same and contains the following main sections:
- program header;
- section for connecting modules used in the program;
- descriptions section;
- operators section.
Of the listed sections, the operator section is required. All other sections (if allowed by the program) are optional and can be omitted.
⇑
3.1. Program header (program)
The program header is declared as follows
program ProgramName(parameters);
here
- ProgramName – the name of the program, which is set by the programmer;
- parameters – parameters that the program receives.
Most often, a program is declared without parameters
program ProgramName;
If the program receives parameters in the form of streams (files) of input/output, then the header can be, for example, like this
program ProgramName(Output, Input, FileName);
⇑
3.2. The section of including units (uses)
This section starts with the uses keyword and has the following general form
uses ModuleName1, ModuleName2, ..., ModuleNameN;
here
- ModuleName1, ModuleName2, ModuleNameN – names of modules connected to the program.
For example, when creating an application of type VCL Forms Application in Delphi system, the uses section looks like this
...
uses
Windows, Messages, Variants, Classes, Graphics, Controls, Forms, Dialogs;
...
⇑
3.3. Descriptions section
The descriptions section consists of several parts, which, in turn, also describe:
- labels (label);
- constants (const);
- types (type);
- variables (var);
- procedures and functions (procedure, function).
All of the above sections can be declared in any order. However, the following order of sections is recommended: label, const, type, var, procedure, or function. This order corresponds to the logic of building programs in Pascal.
You can repeat the label, const, type, var, procedure and function sections (examples below).
The operator section is surrounded by the begin … end operator brackets and has the following form
begin
...
end.
Figure 3 shows the structure of a Pascal program for a console application.
Figure 3. The structure of the console application in Pascal. Declaring all sections
⇑
4. An example of a console application that declares arbitrary elements in the description section
The example demonstrates the possibility of arbitrary order of sections label, const, type, var, function, procedure. A console application in the Delphi programming system is considered.
program Project1; {$APPTYPE CONSOLE} // The uses section must be first and only one. // The following sections can follow one another in a random order. uses SysUtils; // Some function - inverts a string function ReverseString(s : string) : string; var s_reverse : string; i: Integer; begin s_reverse := ''; for i := 1 to length(s) do s_reverse := s[i] + s_reverse; ReverseString := s_reverse; end; // Variable declaration section var f:Integer; // Section of labels description label m1; // Section of constant description const Max = 220; // Another section of the description of constants const Min = 100; // Another variable declaration section. var z : Char = 'z'; // Declare a type declaration section type MyInt = Integer; // Declare a variable of type MyInt var mi : MyInt = 288; begin // Print the values of declared constants Writeln('Max = ', Max); Writeln('Min = ', Min); // Print the values of declared variables Writeln('z = ', z); Writeln('mi = ', mi); // Print the result of a function Writeln(ReverseString('ABCD')); Readln; end.
Test example
Max = 220 Min = 100 z = z mi = 288 DCBA
⇑
Related topics
- The application of VCL Forms Application in Delphi. The Application project in Lazarus
- Principles of structured programming. Modular software development
⇑