Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming

Array as a Function Return Type and Method Parameter

By Zarko Gajic, About.com

Arrays in Delphi allow us to refer to a series of variables by the same name and to use a number (an index) to tell them apart.

Here's an example integer array that can hold up to 7 (integer) values. Note: this is a fixed-size static Delphi array declaration.

var DayVisitors : array[0..6] of Integer;

Arrays as Function Return Types

In Delphi, functions are routines that return a value.

When you want a function to return an array type variable, you might be tempted to use the next declaration:

function GetWeekTotal(weekIndex : integer) : array[0..6] of integer;
begin
  //this will NOT compile
end;
When you try to compile this code, you'll get the next compile-time error: [Pascal Error] E2029 Identifier expected but 'ARRAY' found.

Obviously, when you declare functions that will return array value, you cannot include index type specifiers return declaration.

In order to allow a function to return an array value, you first need to create a custom array type, then use it as a return function type:

//this WILL compile
type
  TDayVisitors = array[0..6] of integer;

...

function GetWeekTotal(weekIndex : integer) : TDayVisitors;
begin
  //do some calculation for the provided "week"
end;

Arrays as Method/Routine Properties

Similar to using arrays as function return types, when you declare routines that take array parameters, you cannot include index type specifiers in the parameter declarations.
type
  TDayVisitors = array[0..6] of integer;

...

procedure DisplayWeekTotal(weekVisitors : TDayVisitors) ;
begin
  //display some info for the provided "week"
end;

Delphi tips navigator:
» Is the Current Windows user a Computer Administrator
« Drop Multiple Instances of a Component on a Delphi Form

More Delphi Programming Quick Tips
Zarko Gajic
Guide since 1998

Zarko Gajic
Delphi Programming Guide

Explore Delphi Programming

About.com Special Features

Build Your Own Website

Step-by-step advice on how to do everything from choosing a Web host to promoting your content. More >

Connect Your Home Computers

Easy ways to connect two computers for networking purposes. More >

Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. Delphi 2007 Tips
  7. Array as a Function Return Type and Method Parameter in Delphi

©2009 About.com, a part of The New York Times Company.

All rights reserved.