Originale-mail to me for new edition

 

Open array parameters

 

Open array parameters allow arrays of different sizes to be passed to the same procedure or function. To define a routine with an open array parameter, use the syntax array of type (rather than array[X..Y] of type) in the parameter declaration. For example,

function Find(A: array of Char): Integer;

declares a function called Find that takes a character array of any size and returns an integer.

Note: The syntax of open array parameters resembles that of dynamic array types, but they do not mean the same thing. The example above creates a function that takes any array of Char elements, including (but not limited to) dynamic arrays. To declare parameters that must be dynamic arrays, you need to specify a type identifier:

type TDynamicCharArray = array of Char;

function Find(A: TDynamicCharArray): Integer;

For information about dynamic arrays, see Dynamic arrays.

Within the body of a routine, open array parameters are governed by the following rules.

·They are always zero-based. The first element is 0, the second element is 1, and so forth. The standard Low and High functions return 0 and Length -1, respectively. The SizeOf function returns the size of the actual array passed to the routine.

·They can be accessed by element only. Assignments to an entire open array parameter are not allowed.

·They can be passed to other procedures and functions only as open array parameters or untyped var parameters. They cannot be passed to SetLength.

·Instead of an array, you can pass a variable of the open array parameter’s base type. It will be treated as an array of length 1.

When you pass an array as an open array value parameter, the compiler creates a local copy of the array within the routine’s stack frame. Be careful not to overflow the stack by passing large arrays.

The following examples use open array parameters to define a Clear procedure that assigns zero to each element in an array of reals and a Sum function that computes the sum of the elements in an array of reals.

procedure Clear(var A: array of Real);

var

  I: Integer;

begin

  for I := 0 to High(A) do A[I] := 0;

end;

function Sum(const A: array of Real): Real;

var

  I: Integer;

  S: Real;

begin

  S := 0;

  for I := 0 to High(A) do S := S + A[I];

  Sum := S;

end;

When you call routines that use open array parameters, you can pass open array constructors to them.

Variant open array parameters

 

Topic groups

 

See also

Array parameters: Overview

Open array constructors

Parameters: Overview

Procedures and functions: Overview

Variant open array parameters

 

 

译文

 

开放数组参数

 

开放数组参数允许不同尺寸的数组传递给相同的过程或函数。要定义含有开放数组参数的例程,需要在参数声明中使用语法array of type(优于array[X..Y] of type)。例如,

function Find(A: array of Char): Integer;

上面声明了一个叫做Find的函数,该函数接受任何尺寸的字符数组并返回一个整数。

注意:开放数组参数的语法类似于动态数组类型,但它们的用途完全不同。上面的例子创建的函数接受任何以字符为元素的数组,包括(但不限于)动态字符数组。要声明必需是动态数组的参数,则需要指定一个类型标识符:

type TDynamicCharArray = array of Char;

function Find(A: TDynamicCharArray): Integer;

有关动态数组的更多信息,见动态数组

在例程主体中,开放数组参数受控于下列规则:

·总是零基数组。即第一个元素的索引是0,第二个元素的索引是1,等等。标准函数LowHigh分别返回0Length -1。函数SizeOf返回传递给例程的实际数组的尺寸。

·只能访问数组中的元素。对整个开放数组参数赋值是不允许的。

·They can be passed to other procedures and functions only as open array parameters or untyped var parameters. They cannot be passed to SetLength.

·只能作为开放数组参数或无类型的变量(var)参数被传递到其他过程或函数。不能传递到标准过程SetLength

·可以代替数组,传递一个开放数组参数基类型的变量,该变量将被视为长度是 1 的数组。

当把一个数组作为开放数组的值参数传递时,编译器将在例程的栈中创建数组副本。因此在传递大的数组时小心栈溢出。

下面的例子用开放数组参数定义了一个叫做Clear的过程,该过程对数组中的每个元素赋值0;还定义了一个叫做Sum的函数,该函数计算实数数组中所有元素的和。

procedure Clear(var A: array of Real);

var

  I: Integer;

begin

  for I := 0 to High(A) do A[I] := 0;

end;

function Sum(const A: array of Real): Real;

var

  I: Integer;

  S: Real;

begin

  S := 0;

  for I := 0 to High(A) do S := S + A[I];

  Sum := S;

end;

当调用使用了开放数组参数的例程时,可以向其传递开放数组构造器

变体开发数组参数

 

主题组

 

相关主题

数组参数:概述

开放数组构造器

参数:概述

过程和函数:概述

变体开放数组参数