Hi all,

I don't use Delphi often so this is probably a stupid question again, but why does Delphi (7) re-index arrays when they're passed to a function, as a simplified example...

program example;
{$APPTYPE CONSOLE}
uses
  SysUtils;
var
  numbers: array[1..5] of Integer = (10, 20, 30, 40, 50);

procedure example(var nums: array of Integer);
begin
  WriteLn(nums[5]); //Undefined. 50 -> nums[4]
end;

begin
  Writeln(numbers[5]);
  example(numbers);
  Readln;
end.

Why does it do this? Thanks in advance for any answers.

Recommended Answers

All 2 Replies

Hello
this happend because an array index in Pascal by default start from 0
your array in Main program Start From 1 But the array that is defined in procedure is started from 0 By Default
so if you want to print fifth element of array in procedure you mast print num[4] in procedure.

What if the added parameter type is not meet properly?
Listen,if you create a new type as array and pass it to the proc...

program array1___;
{$APPTYPE CONSOLE}
uses
  SysUtils;

type TArray = array[1..5]of integer;

const numbers:TArray = (10,20,30,40,50);


procedure example(const nums: TArray);
begin
  WriteLn(nums[5]);
end;

begin
  Writeln(numbers[5]);{result is 50}
  example(numbers);{here too}
  Readln;
end.

:D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.