Exit: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: Exit
| Old | New | Line | |
|---|---|---|---|
| 1 | + | The pseudo-[[Procedure|procedure]] **[`exit`](https://www.freepascal.org/docs-html/rtl/system/exit.html)** immediately leaves the surrounding [[Block|block]]. It is similar to [[Pascal for C users|C]]’s `return`. | |
| 2 | + | ||
| 3 | + | ## behavior | |
| 4 | + | ||
| 5 | + | `Exit` is an [[UCSD Pascal]] extension. As of FPC version 3.2.0 it is available in *all* [[Compiler Mode|compiler compatibility modes]]. | |
| 6 | + | ||
| 7 | + | ### basic | |
| 8 | + | ||
| 9 | + | Invoking `exit` has the same effect as a [[Goto|goto]] to an invisible [[Label|label]] right before the block’s [[End|end]]. The `program` | |
| 10 | + | ||
| 11 | + | ```delphi | |
| 12 | + | program exitDemo(input, output, stdErr); | |
| 13 | + | var | |
| 14 | + | i: integer; | |
| 15 | + | begin | |
| 16 | + | readLn(i); | |
| 17 | + | if i = 0 then | |
| 18 | + | begin | |
| 19 | + | exit; | |
| 20 | + | end; | |
| 21 | + | writeLn(123 div i); | |
| 22 | + | end. | |
| 23 | + | ``` | |
| 24 | + | ||
| 25 | + | is effectively identical to | |
| 26 | + | ||
| 27 | + | ``` pascal | |
| 28 | + | program exitDemo(input, output, stdErr); | |
| 29 | + | label | |
| 30 | + | 9999; | |
| 31 | + | var | |
| 32 | + | i: integer; | |
| 33 | + | begin | |
| 34 | + | readLn(i); | |
| 35 | + | if i = 0 then | |
| 36 | + | begin | |
| 37 | + | goto 9999; | |
| 38 | + | end; | |
| 39 | + | writeLn(123 div i); | |
| 40 | + | 9999: | |
| 41 | + | end. | |
| 42 | + | ``` | |
| 43 | + | ||
| 44 | + | Confer the respective [[Assembly language|assembly language]] output. In a manner of speaking, using `exit` merely avoids the “taboo word” *`goto`*. | |
| 45 | + | ||
| 46 | + | ### functions | |
| 47 | + | ||
| 48 | + | *Inside* a [[Function|function]] definition `exit` optionally accepts one argument. This argument must be assignment-compatible to and defines the functions result before actually transferring control to the function’s call site. | |
| 49 | + | ||
| 50 | + | ```delphi | |
| 51 | + | function ackermann(const m, n: ALUUInt): ALUUInt; | |
| 52 | + | begin | |
| 53 | + | if m = 0 then | |
| 54 | + | begin | |
| 55 | + | exit(n + 1); | |
| 56 | + | end; | |
| 57 | + | if n = 0 then | |
| 58 | + | begin | |
| 59 | + | exit(ackermann(m - 1, 1)); | |
| 60 | + | end; | |
| 61 | + | exit(ackermann(m - 1, ackermann(m, n - 1))); | |
| 62 | + | end; | |
| 63 | + | ``` | |
| 64 | + | ||
| 65 | + | In this example the line | |
| 66 | + | ||
| 67 | + | ```delphi | |
| 68 | + | exit(n + 1); | |
| 69 | + | ``` | |
| 70 | + | ||
| 71 | + | is equivalent to | |
| 72 | + | ||
| 73 | + | ```delphi | |
| 74 | + | ackermann := n + 1; | |
| 75 | + | exit; | |
| 76 | + | ``` | |
| 77 | + | ||
| 78 | + | ### exceptions | |
| 79 | + | ||
| 80 | + | Any accompanying [[Finally|finally]] frame is executed before actually jumping to the block’s `end`. Consider the following example: | |
| 81 | + | ||
| 82 | + | ```delphi | |
| 83 | + | program tryExitDemo(input, output, stdErr); | |
| 84 | + | {$modeSwitch exceptions+} | |
| 85 | + | begin | |
| 86 | + | try | |
| 87 | + | exit; | |
| 88 | + | writeLn('Try.'); | |
| 89 | + | finally | |
| 90 | + | writeLn('Finally.'); | |
| 91 | + | end; | |
| 92 | + | writeLn('Bye.'); | |
| 93 | + | end. | |
| 94 | + | ``` | |
| 95 | + | ||
| 96 | + | This `program` outputs *one* line: | |
| 97 | + | ||
| 98 | + | ```text | |
| 99 | + | Finally. | |
| 100 | + | ``` | |
| 101 | + | ||
| 102 | + | > **Note:** `Exit` may not appear in a `finally` [[Frame|frame]]. | |
| 103 | + | ||
| 104 | + | ## notes | |
| 105 | + | ||
| 106 | + | - `Exit` is a regular [[Identifier|identifier]]. You *can* redefine it. You can refer to its special meaning via the FQI (fully-qualified identifier) `system.exit` at any time. NB: `Exit` is compiler intrinsic and not actually defined in the [[System unit|system unit]]. | |
| 107 | + | - `Exit` can be implemented as an unconditional `jmp` instruction. In higher [[Optimization|optimization]] levels it might get eliminated. | |
| 108 | + | - The [[FPC]] does not support *naming* the block to leave like the [[GNU Pascal]] Compiler does. FPC’s implementation will always leave the *closest containing* block. GPC’s implementation allows to leave *any* surrounding block by supplying the respective [[Routine|routine]]’s name as an argument. | |
| 109 | + | ||
| 110 | + | ## see also | |
| 111 | + | ||
| 112 | + | - [[Break|break]] – leave a [[Loops|loop]] | |
| 113 | + | - [[halt]] – terminate an entire [[Program|program]] | |
| 114 | + | - [`system.exitCode`](https://www.freepascal.org/docs-html/rtl/system/exitcode.html) | |
| 115 | + | ||
| 116 | + | --- | |
| 117 | + | *From the Free Pascal wiki, page "Exit", as it stood on 2022-01-22 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).* | |
| 118 | + | Category: Code |
