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
forwarddirective is for mutually recursive subroutines .All subroutine declarations in a unit’s interface section are already forward declarations, so Delphi ignores the
forwarddirective in a unit’s interface section.A class declaration declares the signatures for all the methods of the class, so do not use the
forwarddirective for methods.You can also declare a class type as a forward declaration, but you don’t use the
forwarddirective. See theclasskeyword 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 ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access