Sign in Join
Wiki copytemporary

Exit

Deutsch · English

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

delphi
program exitDemo(input, output, stdErr);
var
    i: integer;
begin
    readLn(i);
    if i = 0 then
    begin
        exit;
    end;
    writeLn(123 div i);
end.

is effectively identical to

pascal
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.

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.

delphi
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;

In this example the line

delphi
        exit(n + 1);

is equivalent to

delphi
        ackermann := n + 1;
        exit;

exceptions

Any accompanying finally frame is executed before actually jumping to the block’s end. Consider the following example:

delphi
program tryExitDemo(input, output, stdErr);
{$modeSwitch exceptions+}
begin
    try
        exit;
        writeLn('Try.');
    finally
        writeLn('Finally.');
    end;
    writeLn('Bye.');
end.

This program outputs one line:

text
Finally.

Note: Exit may not appear in a finally frame.

notes

see also


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.

Revision 1, last changed by WikiArchive .

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.