From Lazarus wiki
Revision as of 13:53, 17 May 2022 by Zoltanleo (talk | contribs)
Jump to navigationJump to search

Deutsch (de) English (en) suomi (fi) français (fr) Bahasa Indonesia (id) italiano (it) português (pt) русский (ru)

Понятие программа означает либо исполняемая программа, т.е. самодостаточное и запускаемое приложение, либо часть файла (файлов) с исходным кодом на языке Pascal, который может быть скомпилирован и не объявлен в виде модуля или библиотеки. Иногда оно называется главной программой.

Главная программа

program is a reserved word that introduces a classical program source code file:

program hiWorld(input, output, stdErr);


Meanwhile FPC discards the program header, i. e. the first line. The output file name is determined by the source code file’s name. However, the program name does become a reserved identifier (except in the ISO modes [since FPC 3.3.1/trunk revision #45757; cf. Issue #37322]). In the example above, e.g. attempting to define a constant named hiWorld would trigger a duplicate identifier compile time error. The program name will identify the global scope, thus it can be used to write fully-qualified identifiers.

The file descriptor list is completely ignored, except in {$mode ISO}. The text variables input, output and stderr are always opened and their names can not be changed in other modes. (cf.: SysInitStdIO is always called in rtl/linux/system.pp)

Therefore with FPC the following complete source code example compiles identically as does the previous example.


If the program is syntactically correct, FPC ignores anything that comes after the final end.. The following will compile without problems:

program awesomeProgram(input, output, stdErr);
end.I thank my mom, my dad, and everyone who supported me in making this program.

This “feature” is primarily used to supply an in-file changelog or copyright notice.

FPC does not support multiple modules in one source code file, like some other compilers did or do. Each module source code has to reside in its own file. However, the restriction that module names have to match file names does not apply to programs. This is due to the fact that programs cannot be included by other modules, thus searching them (via their file name) is not necessary.

Program structure

A program file has to follow a certain structure.

  1. A (depending on used compiler possibly optional) program header.
  2. There can be at most one uses-clause and it has to be at the top of the program right after the program header.
  3. Exactly one block that concludes with an end. (note the period). This block may contain – in contrast to regular blocks – resourcestring section(s).

The exact order and number of various sections after the (optional) uses-clause until final the compound statement beginend. is free of choice.

However, there are some plausible considerations.

  • A type-section comes prior any section that can use types, e.g. var-sections or routine declarations.
  • Since goto is known as the devil’s tool, a label-section, if any, is as close as possible to the statement-frame it is supposed to declare labels for.
  • Generally you go from general into specifics: For example a var-section comes in front of a threadVar-section. A const-section comes before a resourceString-section.
  • resourceString-sections can be either static or global, that means they should appear relatively soon after the uses-clause.
  • Direct usage of global variables in routines (or even the mere possibility) is considered as bad style. Instead, declare/define your routines prior any var-(like)-section. (beware: play it safe and set {$writeableConst off})
  • Global compiler directives, especially such that allow or restrict what can be written (e.g. {$goto on} allows the use of goto) or implicitly add unit dependencies like {$mode objFPC} should appear soon after the program header.

Taking all considerations into account the rough program structure should look like this (except for label and {$goto on} which are only mentioned for the sake of completeness):

program sectionDemo(input, output, stdErr);

// Global compiler directives ----------------------------
{$mode objFPC}
{$goto on}


	answer = 42;

	helloWorld = 'Hello world!';

	primaryColor = (red, green, blue);

procedure doSomething(const color: primaryColor);

// M A I N -----------------------------------------------
	i: longint;

	z: longbool;


[The example consciously ignores the possibility of “typed constants”, sticking rather to traditional concepts than unnecessarily confusing beginners.]

See also