A function declaration is like a
procedure declaration except that it specifies a return type and a return
value. Function declarations have the form
function functionName(parameterList):
returnType; directives;
localDeclarations;
begin
statements
end;
where functionName is any valid
identifier, returnType is any type, statements is a sequence of
statements that execute when the function is called, and (parameterList),
directives;, and localDeclarations; are optional.
·For information about the parameterList, see Parameters.
·For information about directives, see Calling conventions, Forward and interface
declarations, External declarations,
Overloading procedures
and functions, and Writing
dynamically loadable libraries. If you include more than one directive,
separate them with semicolons.
·For information about localDeclarations,
which declares local identifiers, see Local
declarations.
The
function’s statement block is governed by the same rules that apply to
procedures. Within the statement block, you can use variables and other
identifiers declared in the localDeclarations part of the function,
parameter names from the parameter list, and any identifiers within whose scope
the function declaration falls. In addition, the function name itself acts as a
special variable that holds the function’s return value, as does the predefined
variable Result.
For
example,
function
WF: Integer;
begin
WF := 17;
end;
defines a constant function called WF that
takes no parameters and always returns the integer value 17. This declaration
is equivalent to
function
WF: Integer;
begin
Result := 17;
end;
Here is a more complicated function
declaration:
function
Max(A: array of Real; N: Integer): Real;
var
X: Real;
I: Integer;
begin
X := A[0];
for I := 1 to N - 1 do
if X < A[I] then
X := A[I];
Max := X;
end;
You can
assign a value to Result or to the function name repeatedly within a
statement block, as long as you assign only values that match the declared
return type. When execution of the function terminates, whatever value was last
assigned to Result or to the function name becomes the function’s return
value. For example,
function
Power(X: Real; Y: Integer): Real;
var
I: Integer;
begin
Result := 1.0;
I := Y;
while I > 0 do
begin
if Odd(I) then
Result := Result * X;
I := I div 2;
X := Sqr(X);
end;
end;
Result and the function name always represent the same value. Hence
function
MyFunction: Integer;
begin
MyFunction := 5;
Result := Result * 2;
MyFunction := Result + 1;
end;
returns the value 11. But Result is not
completely interchangeable with the function name. When the function name
appears on the left side of an assignment statement, the compiler assumes that
it is being used (like Result) to track the return value; when the
function name appears anywhere else in the statement block, the compiler
interprets it as a recursive call to the function itself. Result, on the
other hand, can be used as a variable in operations, typecasts, set
constructors, indexes, and calls to other routines.
As long
as extended syntax is enabled ({$X+}), Result is implicitly
declared in every function. Do not try to redeclare it.
If
execution terminates without an assignment being made to Result or the
function name, then the function’s return value is undefined.
Overloading procedures and functions
Declaring procedures and
functions: Overview
函数声明与过程声明相似。不同的是,函数声明需要指定返回类型和返回值。函数声明具有如下形式
function functionName(parameterList):
returnType; directives;
localDeclarations;
begin
statements
end;
这里的functionName是任意有效的标识符,返回类型returnType是任何类型,语句statements是函数被调用时用于执行的语句序列,参数列表(parameterList)、指示字directives和局部声明localDeclarations是可选的。
·有关参数列表parameterList的更多信息,见参数。
·有关指示字directives的更多信息,见调用约定,向前声明和接口声明,外部声明,重载过程和函数,以及编写动态可加载库。如果包括多于一个指示字,则要用分号隔开。
·有关局部声明(声明局部的标识符)的更多信息,见局部声明。
函数的语句块由适用于过程的相同规则管理。在语句块中,可以使用在函数局部声明localDeclarations部分中声明的变量和其他标识符,可以使用参数列表中的参数名,可以使用任何作用域覆盖函数声明的标识符。此外,函数名自身表现为保存函数返回值的特殊变量,与预定义变量Result相同。
例如,
function
WF: Integer;
begin
WF := 17;
end;
上面定义了一个叫做WF的常量函数,该函数不接受任何参数并且总是返回整数值17。上面的声明等价于
function WF:
Integer;
begin
Result := 17;
end;
下面是一个更复杂的函数声明:
function
Max(A: array of Real; N: Integer): Real;
var
X: Real;
I: Integer;
begin
X := A[0];
for I := 1 to N - 1 do
if X < A[I] then
X := A[I];
Max := X;
end;
在语句块中,可以多次对Result或函数名赋值,只要所赋的值与声明的返回类型匹配。函数的执行终止时,不管是哪一种,最后赋给Result或函数名的值成为函数的返回值。例如,
function
Power(X: Real; Y: Integer): Real;
var
I: Integer;
begin
Result := 1.0;
I := Y;
while I > 0 do
begin
if Odd(I) then
Result := Result * X;
I := I div 2;
X := Sqr(X);
end;
end;
Result和函数名总是代表相同的值。因此
function
MyFunction: Integer;
begin
MyFunction := 5;
Result := Result * 2;
MyFunction := Result + 1;
end;
该函数返回值11。然而,用函数名不是完全可以改变Result。当函数名出现在赋值语句的左边时,编译器认为它作为返回值被使用(此时和Result一样);当函数名出现在语句块的其他任何位置时,编译器将其作为对函数自身的递归调用。另一方面,Result可以作为变量用于运算、类型转换、集合构造器、索引、对其他例程的调用。
扩展语法编译指示({$X+})有效时,Result在所有函数中都隐含声明。不要试图对其再声明。
如果执行终止时未曾向Result或函数名赋值,那么函数的返回值是未定义的。