Difference between revisions of "Exit"
(English translation of German page) |
(add category code) |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
{{exit}} | {{exit}} | ||
+ | The pseudo-[[Procedure|<syntaxhighlight lang="pascal" inline>procedure</syntaxhighlight>]] '''{{Doc|package=RTL|unit=system|identifier=exit|text=<syntaxhighlight lang="delphi" inline>exit</syntaxhighlight>}}''' immediately leaves the surrounding [[Block|block]]. | ||
+ | It is similar to [[Pascal for C users|C]]’s <syntaxhighlight lang="c" inline>return</syntaxhighlight>. | ||
− | + | == behavior == | |
+ | <syntaxhighlight lang="delphi" inline>Exit</syntaxhighlight> is an [[UCSD Pascal]] extension. | ||
+ | As of FPC version 3.2.0 it is available in ''all'' [[Compiler Mode|compiler compatibility modes]]. | ||
+ | === basic === | ||
+ | Invoking <syntaxhighlight lang="delphi" inline>exit</syntaxhighlight> has the same effect as a [[Goto|<syntaxhighlight lang="pascal" inline>goto</syntaxhighlight>]] to an invisible [[Label|<syntaxhighlight lang="pascal" inline>label</syntaxhighlight>]] right before the block’s [[End|<syntaxhighlight lang="pascal" inline>end</syntaxhighlight>]]. | ||
+ | The <syntaxhighlight lang="pascal" inline>program</syntaxhighlight> | ||
+ | <syntaxhighlight lang="delphi" line highlight="8"> | ||
+ | program exitDemo(input, output, stdErr); | ||
+ | var | ||
+ | i: integer; | ||
+ | begin | ||
+ | readLn(i); | ||
+ | if i = 0 then | ||
+ | begin | ||
+ | exit; | ||
+ | end; | ||
+ | writeLn(123 div i); | ||
+ | end. | ||
+ | </syntaxhighlight> | ||
+ | is effectively identical to | ||
+ | <syntaxhighlight lang="pascal" line highlight="2-3,10,13"> | ||
+ | program exitDemo(input, output, stdErr); | ||
+ | label | ||
+ | 9999; | ||
+ | var | ||
+ | i: integer; | ||
+ | begin | ||
+ | readLn(i); | ||
+ | if i = 0 then | ||
+ | begin | ||
+ | goto 9999; | ||
+ | end; | ||
+ | writeLn(123 div i); | ||
+ | 9999: | ||
+ | end. | ||
+ | </syntaxhighlight> | ||
+ | Confer the respective [[Assembly language|assembly language]] output. | ||
+ | In a manner of speaking, using <syntaxhighlight lang="delphi" inline>exit</syntaxhighlight> merely avoids the “taboo word” ''<syntaxhighlight lang="pascal" inline>goto</syntaxhighlight>''. | ||
+ | |||
+ | === functions === | ||
+ | ''Inside'' a [[Function|<syntaxhighlight lang="pascal" inline>function</syntaxhighlight>]] definition <syntaxhighlight lang="delphi" inline>exit</syntaxhighlight> 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. | ||
+ | <syntaxhighlight lang="delphi" line start="3"> | ||
+ | function ackermann(const m, n: ALUUInt): ALUUInt; | ||
+ | begin | ||
+ | if m = 0 then | ||
+ | begin | ||
+ | exit(n + 1); | ||
+ | end; | ||
+ | if n = 0 then | ||
+ | begin | ||
+ | exit(ackermann(m - 1, 1)); | ||
+ | end; | ||
+ | exit(ackermann(m - 1, ackermann(m, n - 1))); | ||
+ | end; | ||
+ | </syntaxhighlight> | ||
+ | In this example the line | ||
+ | <syntaxhighlight lang="delphi" line start="7"> | ||
+ | exit(n + 1); | ||
+ | </syntaxhighlight> | ||
+ | is equivalent to | ||
+ | <syntaxhighlight lang="delphi" line start="7"> | ||
+ | ackermann := n + 1; | ||
+ | exit; | ||
+ | </syntaxhighlight> | ||
− | + | === exceptions === | |
+ | Any accompanying [[Finally|<syntaxhighlight lang="delphi" inline>finally</syntaxhighlight>]] frame is executed before actually jumping to the block’s <syntaxhighlight lang="pascal" inline>end</syntaxhighlight>. | ||
+ | Consider the following example: | ||
+ | <syntaxhighlight lang="delphi" line> | ||
+ | program tryExitDemo(input, output, stdErr); | ||
+ | {$modeSwitch exceptions+} | ||
+ | begin | ||
+ | try | ||
+ | exit; | ||
+ | writeLn('Try.'); | ||
+ | finally | ||
+ | writeLn('Finally.'); | ||
+ | end; | ||
+ | writeLn('Bye.'); | ||
+ | end. | ||
+ | </syntaxhighlight> | ||
+ | This <syntaxhighlight lang="pascal" inline>program</syntaxhighlight> outputs ''one'' line: | ||
+ | <syntaxhighlight lang="text"> | ||
+ | Finally. | ||
+ | </syntaxhighlight> | ||
+ | {{Note|<syntaxhighlight lang="delphi" inline>Exit</syntaxhighlight> may not appear in a <syntaxhighlight lang="delphi" inline>finally</syntaxhighlight> [[Frame|frame]].}} | ||
− | + | == notes == | |
+ | * <syntaxhighlight lang="delphi" inline>Exit</syntaxhighlight> is a regular [[Identifier|identifier]]. You ''can'' redefine it. You can refer to its special meaning via the FQI (fully-qualified identifier) <syntaxhighlight lang="delphi" inline>system.exit</syntaxhighlight> at any time. NB: <syntaxhighlight lang="delphi" inline>Exit</syntaxhighlight> is compiler intrinsic and not actually defined in the [[System unit|<syntaxhighlight lang="delphi" inline>system</syntaxhighlight> <syntaxhighlight lang="delphi" inline>unit</syntaxhighlight>]]. | ||
+ | * <syntaxhighlight lang="delphi" inline>Exit</syntaxhighlight> can be implemented as an unconditional <syntaxhighlight lang="asm" inline>jmp</syntaxhighlight> instruction. In higher [[Optimization|optimization]] levels it might get eliminated. | ||
+ | * 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. | ||
− | <syntaxhighlight lang= | + | == see also == |
− | + | * [[Break|<syntaxhighlight lang="delphi" inline>break</syntaxhighlight>]] – leave a [[Loops|loop]] | |
− | + | * [[halt|<syntaxhighlight lang="pascal" inline>halt</syntaxhighlight>]] – terminate an entire [[Program|<syntaxhighlight lang="pascal" inline>program</syntaxhighlight>]] | |
− | + | * {{Doc|package=RTL|unit=system|identifier=exitcode|text=<syntaxhighlight lang="delphi" inline>system.exitCode</syntaxhighlight>}} | |
− | + | ||
− | + | [[Category: Code]] | |
− | |||
− | |||
− | . | ||
− | </syntaxhighlight> |
Latest revision as of 23:08, 22 January 2022
│
Deutsch (de) │
English (en) │
The pseudo-procedure
exit
immediately leaves the surrounding block.
It is similar to C’s return
.
behavior
Exit
is an UCSD Pascal extension.
As of FPC version 3.2.0 it is available in all compiler compatibility modes.
basic
Invoking exit
has the same effect as a goto
to an invisible label
right before the block’s end
.
The program
1program exitDemo(input, output, stdErr);
2var
3 i: integer;
4begin
5 readLn(i);
6 if i = 0 then
7 begin
8 exit;
9 end;
10 writeLn(123 div i);
11end.
is effectively identical to
1program exitDemo(input, output, stdErr);
2label
3 9999;
4var
5 i: integer;
6begin
7 readLn(i);
8 if i = 0 then
9 begin
10 goto 9999;
11 end;
12 writeLn(123 div i);
139999:
14end.
Confer the respective assembly language output.
In a manner of speaking, using exit
merely avoids the “taboo word” goto
.
functions
Inside a 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.
3function ackermann(const m, n: ALUUInt): ALUUInt;
4begin
5 if m = 0 then
6 begin
7 exit(n + 1);
8 end;
9 if n = 0 then
10 begin
11 exit(ackermann(m - 1, 1));
12 end;
13 exit(ackermann(m - 1, ackermann(m, n - 1)));
14end;
In this example the line
7 exit(n + 1);
is equivalent to
7 ackermann := n + 1;
8 exit;
exceptions
Any accompanying finally
frame is executed before actually jumping to the block’s end
.
Consider the following example:
1program tryExitDemo(input, output, stdErr);
2{$modeSwitch exceptions+}
3begin
4 try
5 exit;
6 writeLn('Try.');
7 finally
8 writeLn('Finally.');
9 end;
10 writeLn('Bye.');
11end.
This program
outputs one line:
Finally.
notes
Exit
is a regular 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 thesystem
unit
.Exit
can be implemented as an unconditionaljmp
instruction. In higher optimization levels it might get eliminated.- 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’s name as an argument.
see also
break
– leave a loophalt
– terminate an entireprogram
system.exitCode