The forward
directive replaces the block, including local variable declarations and
statements, in a procedure or function declaration. For example,
function
Calculate(X, Y: Integer): Real; forward;
declares a function called Calculate.
Somewhere after the forward declaration, the routine must be redeclared
in a defining declaration that includes a block. The defining
declaration for Calculate might look like this:
function
Calculate;
... { declarations }
begin
... { statement block }
end;
Ordinarily,
a defining declaration does not have to repeat the routine’s parameter list or
return type, but if it does repeat them, they must match those in the forward
declaration exactly (except that default parameters can be omitted). If the forward
declaration specifies an overloaded procedure or function (see Overloading procedures
and functions), then the defining declaration must repeat the parameter list.
Between a
forward declaration and its defining declaration, you can place nothing
except other declarations. The defining declaration can be an external
or assembler declaration, but it cannot be another forward
declaration.
The
purpose of a forward declaration is to extend the scope of a procedure
or function identifier to an earlier point in the source code. This allows
other procedures and functions to call the forward-declared routine
before it is actually defined. Besides letting you organize your code more
flexibly, forward declarations are sometimes necessary for mutual
recursions.
The forward directive is not allowed in the interface section of a unit. Procedure and function headers in the interface section, however, behave like forward declarations and must have defining declarations in the implementation section. A routine declared in the interface section is available from anywhere else in the unit and from any other unit or program that uses the unit where it is declared.
Default
parameters in forward and interface declarations
Inline
assembler code: Overview
The forward directive replaces the block, including local variable declarations and statements, in a procedure or function declaration. For example,
指示字forward置于过程或函数声明的块中,包括包括局部变量声明和语句。例如,
function
Calculate(X, Y: Integer): Real; forward;
这里声明了一个叫做Calculate的函数。在此向前声明之后的某个位置,例程必需在同一个块中通过定义声明(defining declaration)被再声明。函数Calculate的定义声明如下:
function
Calculate;
... { declarations }
begin
... { statement block }
end;
通常,定义声明不必重申例程的参数列表或返回值,但如果要重申,则必需与向前声明完全一致(除了缺省参数可以被忽略)。如果向前声明说明了一个重载的过程或函数(见重载过程和函数),那么相应的定义声明必需重申参数列表。
在向前声明和相应的定义声明之间,除其他声明外,不能放置任何代码。定义声明可以是external或assembler声明,但不能是另一个向前声明。
向前声明的意义在于,在源代码中延伸过程或函数的作用域到更早的点(即向前延伸例程的作用域)。如此以来,其他过程和函数在该例程被真实定义之前就可以调用向前声明了的例程。除了可以弹性地组织代码之外,向前声明有时对于相互递归调用是必要的。
指示字forward不允许出现在单元的接口(interface)节中。然而,接口节中的过程和函数首部,其表现与向前声明相似,必需在实现(implementation)节中有定义声明。接口节中声明的例程,在声明其的单元中的任何位置都是可用的,在所有其他使用了该单元的单元或程序中也是可用的。