Name

Forward Directive

Syntax

Subroutine header; forward;

Description

In a unit’s implementation section or in the body of a program or library, you can declare a function or procedure before you call it by declaring the header (name, parameters, and return type) with the forward directive.

Delphi compiles a file by reading from its beginning to the end. When it reaches a function or procedure call, it must already know the number and type of the subroutine or method parameters and the function’s return type (the subroutine’s signature). Using the forward directive is one way to declare a subroutine early in a file, and define the entire subroutine later.

Tips and Tricks

  • A common use of the forward directive is for mutually recursive subroutines .

  • All subroutine declarations in a unit’s interface section are already forward declarations, so Delphi ignores the forward directive in a unit’s interface section.

  • A class declaration declares the signatures for all the methods of the class, so do not use the forward directive for methods.

  • You can also declare a class type as a forward declaration, but you don’t use the forward directive. See the class keyword for details.

Example

// The WalkDirectory procedure recursively iterates over the files
// in a directory and in its subdirectories. Each file is added to
// a TTreeView control, showing the directory and file hierarchy.

procedure WalkDirectory(const Dir: string; Node: TTreeNode); forward; // Add a single file to the tree view. If the file ...

Get Delphi in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.