Program: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | A **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. | |
| 2 | 2 | ||
| 3 | 3 | ## Main program | |
| 4 | 4 | ||
| 5 | 5 | `program` is a [[Reserved word|reserved word]] that introduces a classical program source code file: | |
| 6 | 6 | ||
| 7 | 7 | ``` pascal | |
| 8 | 8 | program hiWorld(input, output, stdErr); | |
| 9 | 9 | ||
| 10 | 10 | begin | |
| 11 | 11 | writeLn('Hi!'); | |
| 12 | 12 | end. | |
| 13 | 13 | ``` | |
| 14 | 14 | ||
| 15 | - | ||
| 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. | |
| 16 | 16 | ||
| 17 | 17 | The 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)) | |
| 18 | 18 | ||
| 19 | 19 | Therefore with FPC the following complete source code example compiles identically as does the previous example. | |
| 20 | 20 | ||
| 21 | 21 | ``` pascal | |
| 22 | 22 | begin | |
| 23 | 23 | writeLn('Hi!'); | |
| 24 | 24 | end. | |
| 25 | 25 | ``` | |
| 26 | 26 | ||
| 27 | 27 | If the program is syntactically correct, FPC ignores anything that comes after the final `end.`. The following will compile without problems: | |
| 28 | 28 | ||
| 29 | 29 | ``` pascal | |
| 30 | 30 | program awesomeProgram(input, output, stdErr); | |
| 31 | 31 | begin | |
| 32 | 32 | writeLn('Awesome!'); | |
| 33 | 33 | end.I thank my mom, my dad, and everyone who supported me in making this program. | |
| 34 | 34 | ``` | |
| 35 | 35 | ||
| 36 | 36 | This “feature” is primarily used to supply an in-file changelog or copyright notice. | |
| 37 | 37 | ||
| 38 | 38 | 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. | |
| 39 | 39 | ||
| 40 | 40 | ## Program structure | |
| 41 | 41 | ||
| 42 | 42 | A `program` file has to follow a certain [[Basic Pascal Tutorial/Chapter 1/Program Structure|structure]]. | |
| 43 | 43 | ||
| 44 | 44 | 1. A (depending on used compiler possibly optional) program header. | |
| 45 | 45 | 2. 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. | |
| 46 | 46 | 3. Exactly one [[Block|block]] that concludes with an `end.` (note the [[period]]). This block may contain – in contrast to regular blocks – `resourcestring` section(s). | |
| 47 | 47 | ||
| 48 | 48 | The 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. | |
| 49 | 49 | ||
| 50 | 50 | However, there are some plausible considerations. | |
| 51 | 51 | ||
| 52 | 52 | - A [[Type|type-section]] comes prior any section that can use types, e.g. [[Var|var-sections]] or [[Routine|routine]] declarations. | |
| 53 | 53 | - 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. | |
| 54 | 54 | - 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]]. | |
| 55 | 55 | - `resourceString`-sections can be either static or global, that means they should appear relatively soon after the `uses`-clause. | |
| 56 | 56 | - 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}`) | |
| 57 | 57 | - [[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. | |
| 58 | 58 | ||
| 59 | 59 | Taking 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): | |
| 60 | 60 | ||
| 61 | 61 | ```delphi | |
| 62 | 62 | program sectionDemo(input, output, stdErr); | |
| 63 | 63 | ||
| 64 | 64 | // Global compiler directives ---------------------------- | |
| 65 | 65 | {$mode objFPC} | |
| 66 | 66 | {$goto on} | |
| 67 | 67 | ||
| 68 | 68 | uses | |
| 69 | 69 | sysUtils; | |
| 70 | 70 | ||
| 71 | 71 | const | |
| 72 | 72 | answer = 42; | |
| 73 | 73 | ||
| 74 | 74 | resourceString | |
| 75 | 75 | helloWorld = 'Hello world!'; | |
| 76 | 76 | ||
| 77 | 77 | type | |
| 78 | 78 | primaryColor = (red, green, blue); | |
| 79 | 79 | ||
| 80 | 80 | procedure doSomething(const color: primaryColor); | |
| 81 | 81 | begin | |
| 82 | 82 | end; | |
| 83 | 83 | ||
| 84 | 84 | // M A I N ----------------------------------------------- | |
| 85 | 85 | var | |
| 86 | 86 | i: longint; | |
| 87 | 87 | ||
| 88 | 88 | threadVar | |
| 89 | 89 | z: longbool; | |
| 90 | 90 | ||
| 91 | 91 | label | |
| 92 | 92 | 42; | |
| 93 | 93 | begin | |
| 94 | 94 | end. | |
| 95 | 95 | ``` | |
| 96 | 96 | ||
| 97 | - | ||
| 97 | + | \[The example consciously ignores the possibility of “typed constants”, sticking rather to traditional concepts than unnecessarily confusing beginners.\] | |
| 98 | 98 | ||
| 99 | 99 | ## See also | |
| 100 | 100 | ||
| 101 | 101 | - [[Unit|unit]] | |
| 102 | 102 | - [[Library|library]] | |
| 103 | 103 | - [[Basic Pascal Tutorial/Chapter 1/Program Structure|program structure]] in the basic Pascal Introduction series | |
| 104 | 104 | - [§ “Beginning” in the *Pascal Programming* book on Wikibooks.org](https://en.wikibooks.org/wiki/Pascal_Programming/Beginning) | |
| 105 | 105 | ||
| 106 | 106 | --- | |
| 107 | 107 | *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).* |
