0

I really would like to see how it's done, the compiler keeps assuming I have integer indexes and returns errors.

How to pass the following array:

countc: Array['a'..'z'] of Integer;

to a function?

2
  • 1
    Declare a type TMyArray = array['a'..'z'] of Integer; and pass a variable of that type around. Commented Nov 7, 2019 at 15:06
  • I think this may be a problem with the Pascal compiler you are using. It certainly shouldn't be a problem in a correctly implemented Pascal compiler. Commented Nov 7, 2019 at 15:19

1 Answer 1

2

In traditional Pascal, before you can pass something like your array to a function, you have to declare a type that your array is an instance of, like this

type
  TSimpleArray = Array['A'..'Z', '0'..'9'] of integer;
var
  ASimpleArray : TSimpleArray;

In other words, you can't specify the array's bounds in the definition of the function/procedure.

Once you've defined your array type like the above, you can declare a function (or procedure) that has a parameter of the defined type, like this:

function ArrayFunction(SimpleArray : TSimpleArray) : Integer;
var
  C1,
  C2 : Char;
begin
  ArrayFunction := 0;
  for C1 := 'A' to 'Z' do
    for C2 := '0' to '9' do
      ArrayFunction := ArrayFunction + SimpleArray[C1, C2];
end;

which obviously totals the contents of the array.

More modern Pascals like Delphi and FPC's ObjectPascals also support other ways of declaring an array-type parameter, but they have to be zero-based (which precludes the use of char indexes). Delphi and FPC also support the use of `Result' as an alias for the function name, as in

function ArrayFunction(SimpleArray : TSimpleArray) : Integer;
[...]
begin
  Result := 0;

which saves time and effort if you rename a function or copy/paste it to define another function.

Sign up to request clarification or add additional context in comments.

1 Comment

Has this answered your q? If not, please explain why.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.