声明:非Google自动翻译。
英文:
Declaring procedures and functions
When you declare a procedure or function, you specify its name, the number and type of parameters it takes, and, in the case of a function, the type of its return value; this part of the declaration is sometimes called the prototype, heading, or header. Then you write a block of code that executes whenever the procedure or function is called; this part is sometimes called the routine’s body or block.
The standard procedure Exit can occur within the body of any procedure or function. Exit halts execution of the routine where it occurs and immediately passes program control back to the point from which the routine was called.
Procedure declarations
A procedure declaration has the form
procedure procedureName(parameterList); directives;
localDeclarations;
begin
statements
end;
where procedureName is any valid identifier, statements is a sequence of statements that execute when the procedure 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.
Here is an example of a procedure declaration:
procedure NumString(N: Integer; var S: string);
var
V: Integer;
begin
V := Abs(N);
S := '';
repeat
S := Chr(V mod 10 + Ord('0')) + S;
V := V div 10;
until V = 0;
if N < 0 then S := '-' + S;
end;
Given this declaration, you can call the NumString procedure like this:
NumString(17, MyString);
This procedure call assigns the value “17” to MyString (which must be a string variable).
Within a procedure’s statement block, you can use variables and other identifiers declared in the localDeclarations part of the procedure. You can also use the parameter names from the parameter list (like N and S in the example above); the parameter list defines a set of local variables, so don’t try to redeclare the parameter names in the localDeclarations section. Finally, you can use any identifiers within whose scope the procedure declaration falls.
Function declarations
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.
Calling procedures and functions
When you call a procedure or function, program control passes from the point where the call is made to the body of the routine. You can make the call using the routine’s declared name (with or without qualifiers) or using a procedural variable that points to the routine. In either case, if the routine is declared with parameters, your call to it must pass parameters that correspond in order and type to the routine’s parameter list. The parameters you pass to a routine are called actual parameters, while the parameters in the routine’s declaration are called formal parameters.
When calling a routine, remember that
·expressions used to pass typed const and value parameters must be assignment-compatible with the corresponding formal parameters.
·expressions used to pass var and out parameters must be identically typed with the corresponding formal parameters, unless the formal parameters are untyped.
·only assignable expressions can be used to pass var and out parameters.
·if a routine’s formal parameters are untyped, numerals and true constants with numeric values cannot be used as actual parameters.
When you call a routine that uses default parameter values, all actual parameters following the first accepted default must also use the default values; calls of the form SomeFunction(,,X) are not legal.
You can omit parentheses when passing all and only the default parameters to a routine. For example, given the procedure
procedure DoSomething(X: Real = 1.0; I: Integer = 0; S: string = '');
the following calls are equivalent.
DoSomething();
DoSomething;
译文如下:
声明过程和函数
声明过程或函数时,需要为其指定名称、接受参数的个数及类型,对于函数,还要指定返回值的类型,这部分声明有时叫做原型(prototype)、标题(heading)或首部(header)。然后编写代码块,用于过程或函数随时被调用时的执行,这部分声明有时叫做例程的主体(body)或块(block)。
标准过程Exit可以出现在任何过程和函数的块中。Exit过程在其出现的位置停止例程的执行并立即将程序控制交回例程被调用的执行点处。
过程声明
过程声明具有如下形式
procedure procedureName(parameterList); directives;
localDeclarations;
begin
statements
end;
这里的过程名procedureName是任何有效标识符,语句statements是一个当过程被调用时用于执行的语句序列,参数列表(parameterList)、指示字directives;以及局部声明localDeclarations;等是可选的。
·有关参数列表parameterList的更多信息,见参数。
·有关指示字directives的更多信息,见调用约定,向前声明和接口声明,外部声明,重载过程和函数,以及编写动态可加载库。如果包括多于一个指示字,则要用分号隔开。
·有关局部声明(声明局部的标识符)的更多信息,见局部声明。
下面是过程声明的例子:
procedure NumString(N: Integer; var S: string);
var
V: Integer;
begin
V := Abs(N);
S := '';
repeat
S := Chr(V mod 10 + Ord('0')) + S;
V := V div 10;
until V = 0;
if N < 0 then S := '-' + S;
end;
对于上面的声明,可以如下调用NumString过程:
NumString(17, MyString);
该过程将串值 “17” 赋给变量MyString(该变量必需是string类型的变量)。
在过程的语句块中,可以使用在过程的局部声明(localDeclarations)中声明的变量和其他标识符。也可以使用参数列表中的参数名(上面例子中的N和S);参数列表定义了一组局部变量,因此不要试图在局部声明localDeclarations节中再声明参数名。此外,还可以使用任何作用域覆盖过程声明的标识符(此时也称过程声明落在这些标识符的作用域中)。
函数声明
函数声明与过程声明相似。不同的是,函数声明需要指定返回类型和返回值。函数声明具有如下形式
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或函数名赋值,那么函数的返回值是未定义的。
调用过程和函数
调用过程或函数时,程序控制将由调用点传递给例程主体。可以通过使用例程的声明名称(含有或不含限定词)或者通过使用指向例程的程序型变量来实现调用。不管使用哪种方式,如果例程声明中含有参数,那么在调用时必需根据参数列表向例程以正确的顺序和类型传递参数。传递到例程中的参数叫做实际参数(actual parameters),而例程声明中的参数叫做形式参数(formal parameters)。
调用例程时应切记:
·用于传递有类型的常量(const)参数和值(value)参数的表达式必需与相应的形式参数是赋值兼容的。
·用于传递var参数和out参数的表达式,其类型必需与相应形式参数的类型等同,除非形式参数是无类型的。
·只有可赋值的表达式可以被用于传递var参数和out参数。
·如果例程的形式参数是无类型的,那么数字和含数值的真实常量都不能作为实际参数传递。
调用使用了缺省参数值的例程时,跟随在第一个接受缺省值之后的所有实际参数,都必需使用缺省值;形如 SomeFunction(,,X) 的调用是不合法的。
向例程传递所有参数并且所有的参数都使用缺省值时,甚至可以忽略圆括号。例如,对于如下过程
procedure DoSomething(X: Real = 1.0; I: Integer = 0; S: string = '');
下面的调用是等价的:
DoSomething();
DoSomething;
温馨提示:内容为网友见解,仅供参考