Delphi Language Guide Delphi for Microsoft Win32 Delphi for the Microsoft. Net framework



Yüklə 0,84 Mb.
Pdf görüntüsü
səhifə141/294
tarix02.01.2022
ölçüsü0,84 Mb.
#41395
1   ...   137   138   139   140   141   142   143   144   ...   294
DelphiLanguageGuide

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,
123


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 previous example 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;
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 Length1, 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.

Yüklə 0,84 Mb.

Dostları ilə paylaş:
1   ...   137   138   139   140   141   142   143   144   ...   294




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin