14.3 Function results

The result of a function can be set by setting the result variable: this can be the function identifier or, (only in ObjFPC or Delphi mode) the special Result identifier:

Function MyFunction : Integer;

begin
  MyFunction:=12; // Return 12
end;

In Delphi or ObjFPC mode, the above can also be coded as:

Function MyFunction : Integer;

begin
  Result:=12;
end;

As an extension to Delphi syntax, the ObjFPC mode also supports a special extension of the Exit procedure:

Function MyFunction : Integer;

begin
  Exit(12);
end;

The Exit call sets the result of the function and jumps to the final End of the function declaration block. It can be seen as the equivalent of the C return instruction.

Remark Function results are treated as pass-by-reference parameters. That is especially important for managed types: The function result may be non-nil on entry, and set to a valid instance of the type.