Sign in Join
Wiki copytemporary

Program: what changed

Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11A **program** is either an [[Executable program|executable program]], that is, the complete and runnable [[Application|application]], or it is that portion of a [[Pascal]] [[Source code]] [[File|file]] or files that can be compiled and is not declared to be a [[Unit|unit]] or [[Library|library]]. This is sometimes referred to as the main program.
22
33## Main program
44
55`program` is a [[Reserved word|reserved word]] that introduces a classical program source code file:
66
77``` pascal
88program hiWorld(input, output, stdErr);
99
1010begin
1111 writeLn('Hi!');
1212end.
1313```
1414
15-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|identifier]] (except in the <abbr title="International Organization for Standardization">ISO</abbr> modes \[since FPC 3.3.1/trunk revision \#45757; cf. [Issue \#37322](https://bugs.freepascal.org/view.php?id=37322)\]). In the example above, e.g. attempting to define a constant named `hiWorld` would trigger a duplicate identifier [[Compile time|compile time]] error. The program name will identify the global [[Scope|scope]], thus it can be used to write fully-qualified identifiers.
15+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|identifier]] (except in the ISO modes \[since FPC 3.3.1/trunk revision \#45757; cf. [Issue \#37322](https://bugs.freepascal.org/view.php?id=37322)\]). In the example above, e.g. attempting to define a constant named `hiWorld` would trigger a duplicate identifier [[Compile time|compile time]] error. The program name will identify the global [[Scope|scope]], thus it can be used to write fully-qualified identifiers.
1616
1717The file descriptor list is completely ignored, except in [[Mode iso|{$mode ISO}]]. The [[Text|text]] [[Variable|variables]] [`input`](https://www.freepascal.org/docs-html/rtl/system/input.html), [`output`](https://www.freepascal.org/docs-html/rtl/system/output.html) and [`stderr`](https://www.freepascal.org/docs-html/rtl/system/stderr.html) are always opened and their names can not be changed in other modes. (cf.: [`SysInitStdIO`](https://www.freepascal.org/docs-html/rtl/system/sysinitstdio.html) is always called in [`rtl/linux/system.pp`](https://gitlab.com/freepascal.org/fpc/source/-/tree/release_3_0_4/rtl/linux/system.pp#L367-L368))
1818
1919Therefore with FPC the following complete source code example compiles identically as does the previous example.
2020
2121``` pascal
2222begin
2323 writeLn('Hi!');
2424end.
2525```
2626
2727If the program is syntactically correct, FPC ignores anything that comes after the final `end.`. The following will compile without problems:
2828
2929``` pascal
3030program awesomeProgram(input, output, stdErr);
3131begin
3232 writeLn('Awesome!');
3333end.I thank my mom, my dad, and everyone who supported me in making this program.
3434```
3535
3636This “feature” is primarily used to supply an in-file changelog or copyright notice.
3737
3838FPC 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.
3939
4040## Program structure
4141
4242A `program` file has to follow a certain [[Basic Pascal Tutorial/Chapter 1/Program Structure|structure]].
4343
44441. A (depending on used compiler possibly optional) program header.
45452. There can be at most one [[Uses|uses-clause]] and it has to be at the top of the program right after the program header.
46463. Exactly one [[Block|block]] that concludes with an `end.` (note the [[period]]). This block may contain – in contrast to regular blocks – `resourcestring` section(s).
4747
4848The exact order and number of various sections after the (optional) `uses`-clause until final the compound statement [[Begin|begin]]…[[End|end.]] is free of choice.
4949
5050However, there are some plausible considerations.
5151
5252- A [[Type|type-section]] comes prior any section that can use types, e.g. [[Var|var-sections]] or [[Routine|routine]] declarations.
5353- Since [[Goto|goto]] is known as the devil’s tool, a [[Label|label-section]], if any, is as close as possible to the statement-frame it is supposed to declare labels for.
5454- Generally you go from general into specifics: For example a `var`-section comes in front of a [[Threadvar|threadVar-section]]. A [[Const|const-section]] comes before a [[Resourcestring|resourceString-section]].
5555- `resourceString`-sections can be either static or global, that means they should appear relatively soon after the `uses`-clause.
5656- Direct usage of [[Global variables|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}`)
5757- [[global compiler directives|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|{$mode objFPC}]] should appear soon after the program header.
5858
5959Taking all considerations into account the rough program structure should look like this (except for `label` and [[sGoto|{$goto on}]] which are only mentioned for the sake of completeness):
6060
6161```delphi
6262program sectionDemo(input, output, stdErr);
6363
6464// Global compiler directives ----------------------------
6565{$mode objFPC}
6666{$goto on}
6767
6868uses
6969 sysUtils;
7070
7171const
7272 answer = 42;
7373
7474resourceString
7575 helloWorld = 'Hello world!';
7676
7777type
7878 primaryColor = (red, green, blue);
7979
8080procedure doSomething(const color: primaryColor);
8181begin
8282end;
8383
8484// M A I N -----------------------------------------------
8585var
8686 i: longint;
8787
8888threadVar
8989 z: longbool;
9090
9191label
9292 42;
9393begin
9494end.
9595```
9696
97-<small>\[The example consciously ignores the possibility of “typed constants”, sticking rather to traditional concepts than unnecessarily confusing beginners.\]</small>
97+\[The example consciously ignores the possibility of “typed constants”, sticking rather to traditional concepts than unnecessarily confusing beginners.\]
9898
9999## See also
100100
101101- [[Unit|unit]]
102102- [[Library|library]]
103103- [[Basic Pascal Tutorial/Chapter 1/Program Structure|program structure]] in the basic Pascal Introduction series
104104- [§ “Beginning” in the *Pascal Programming* book on Wikibooks.org](https://en.wikibooks.org/wiki/Pascal_Programming/Beginning)
105105
106106---
107107*From the Free Pascal wiki, page "Program", as it stood on 2022-09-02 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history