The body of a function or
procedure often begins with declarations of local variables used in the routine’s
statement block. These declarations can also include constants, types, and
other routines. The scope of a local identifier is limited to the routine where
it is declared.
Functions and procedures sometimes
contain other functions and procedures within the local-declarations section of
their blocks. For example, the following declaration of a procedure called DoSomething
contains a nested procedure.
procedure
DoSomething(S: string);
var
X, Y: Integer;
procedure NestedProc(S: string);
begin
...
end;
begin
...
NestedProc(S);
...
end;
The scope of a nested routine is
limited to the procedure or function in which it is declared. In our example, NestedProc
can be called only within DoSomething.
For real examples of nested routines, look at the DateTimeToString procedure, the ScanDate function, and other routines in the SysUtils unit.
函数或过程的主题通常以用于例程语句块的局部变量的声明开始。这些声明也包括常量、类型以及其他例程。局部标识符的作用域仅限于其声明所在的例程。
函数和过程有时在其所在块中的局部声明部分包含其他函数和过程。例如,下面叫做DoSomething的过程声明中包含了一个嵌套的过程。
procedure
DoSomething(S: string);
var
X, Y: Integer;
procedure NestedProc(S: string);
begin
...
end;
begin
...
NestedProc(S);
...
end;
嵌套例程的作用域仅限于其声明所在的过程或函数。在上面的例子中,NestedProc仅在DoSomething中可以被调用。
关于嵌套例程的实例,可以查阅DateTimeToString过程、ScanDate函数,以及单元SysUtils中的其他例程。