Some special rules apply for passing parameters to a Pascal formal parameter that is either a multidimensional array or an incompletely defined array.
Passing Multidimensional Arrays
Pascal arrays are all stored internally as one-dimensional arrays. Declaring a Pascal array with multiple dimensions creates an indexing compiler scheme, which makes it appear that the array has multiple dimensions. Within the Pascal program, the fact that the array is one-dimensional is never visible. However, this fact is visible when parameters are passed to a Pascal program from a program written in another language.
Because Pascal formal array parameters are implicitly one dimensional, actual array parameters passed to Pascal programs must always be one-dimensional. The elements of the actual array are mapped into the formal array according to an algorithm that increments the indexes for the highest dimension, then the next highest dimension, and so on.
For example, suppose the actual parameter is an ALGOL EBCDIC array of one dimension, [1:27]. The initiating process could pass this parameter to a Pascal formal parameter that is a three-dimensional packed array of char. Suppose each dimension is declared with indexes [1..3]. The following table illustrates the mapping of elements from the ALGOL actual array into the Pascal formal array:
|
ALGOL Index |
Pascal Index |
|---|---|
|
1 |
1,1,1 |
|
2 |
1,1,2 |
|
3 |
1,1,3 |
|
4 |
1,2,1 |
|
5 |
1,2,2 |
|
6 |
1,2,3 |
|
7 |
1,3,1 |
|
8 |
1,3,2 |
|
9 |
1,3,3 |
The initiating process maps the remaining elements in a similar way.
Passing Parameters to Pascal Schemata
Before reading the rules for passing parameters to Pascal schemata, you should understand the following Pascal terms:
-
Index
An index specifies a location in a particular array dimension. If a dimension has indexes running from 1 to 5, then there are five indexes in that dimension.
-
Discriminant
A discriminant appears in an array declaration and specifies the highest-numbered or lowest-numbered index for a particular dimension. If the discriminant is an integer, it is called a constant discriminant. If the discriminant is a variable, it is called a dynamic discriminant.
-
Element
An element is a single location in an array. An element is identified by an index for each dimension stating the element's location in that dimension.
-
Schema
A schema is an array declaration that includes one or more dynamic discriminants. In other words, a schema is a type of incomplete array declaration. Using a schema as a formal parameter makes it possible to pass arrays with different bounds and different numbers of elements to the same formal parameter. The plural of schema is schemata.
When passing an array to a formal parameter that is a Pascal schema, the initiating process must pass one or more additional parameters. This is the only situation in which the system requires that the number of actual parameters be different from the number of formal parameters. The additional actual parameters provide information about the size of the actual array. Each of these additional parameters is a call-by-value integer.
The following are Pascal schemata types and the rules for passing parameters to each of these schemata types:
-
A vlstring (variable-length string). This formal parameter receives two actual parameters: a parameter that contains the string value, followed by a call-by-value integer parameter that records the length of the string.
-
A one-dimensional packed array of char whose upper discriminant is dynamic. This formal parameter receives the following two actual parameters: a one-dimensional array, followed by one call-by-value integer parameter that gives the value of the dynamic discriminant.
-
Any other type of array or packed array whose declaration includes at least one dynamic discriminant. This type of formal parameter receives the following actual parameters, in the order listed:
-
A one-dimensional array of a compatible type.
-
For each dimension, a call-by-value integer parameter specifying the total number of elements in that dimension and all higher dimensions. For example, imagine an array with five indexes in the first dimension, three in the second dimension, and two in the third dimension. The first integer parameter is 30, which is the result of multiplying 5, 3, and 2 together. The second integer parameter is 6, which is the result of multiplying 3 and 2 together. The third integer parameter is 2.
-
For each dynamic discriminant, a call-by-value integer parameter giving the value of the discriminant. The order of the integer parameters is as follows: first-dimension lower discriminant, first-dimension upper discriminant, second-dimension lower discriminant, second-dimension upper discriminant, and so on. Any constant discriminants are omitted.
-
Examples
The following programs illustrate how an ALGOL program can pass an array to a Pascal two-dimensional packed array of char. The ALGOL program passes a one-dimensional EBCDIC array.
% ALGOL PROGRAM
BEGIN
EBCDIC ARRAY
ALGOLARRAY[0:24];
TASK T;
PROCEDURE OUTSIDE(ACTUALARRAY);
EBCDIC ARRAY ACTUALARRAY[*];
EXTERNAL;
REPLACE T.NAME BY “OBJECT/PASCAL/TWODIM/CHAR.”;
REPLACE ALGOLARRAY[0] BY “ONETWOONETWOONETWOONETWO”;
CALL OUTSIDE(ALGOLARRAY) [T];
END.
{ PASCAL PROGRAM }
program pascalarray((formalarray: formalarraytype));
TYPE
indexrange = 1..10;
formalarraytype = packed array [2..5, 2..7] of char;
VAR
arrayindex, arrayindex2: indexrange;
BEGIN
for arrayindex:= 2 to 5 do
for arrayindex2:= 2 to 7 do
formalarray[ arrayindex, arrayindex2 ]:= 'a';
END.The following example shows what would happen if the formal parameter formalarray in the preceding example were changed from a fully specified array to a schema. Because of this change, the ALGOL program must pass additional call-by-value integer parameters.
% ALGOL PROGRAM
BEGIN
EBCDIC ARRAY
ALGOLARRAY [0:24];
INTEGER
ONEDIM, TWODIM, DISC1, DISC2;
TASK T;
PROCEDURE OUTSIDE (ACTUALARRAY, ONEDIM, TWODIM, DISC1, DISC2);
VALUE ONEDIM, TWODIM, DISC1, DISC2;
EBCDIC ARRAY ACTUALARRAY [*];
INTEGER ONEDIM, TWODIM, DISC1, DISC2;
EXTERNAL;
REPLACE T.NAME BY “OBJECT/TASK/SCHEMA/PASCAL/TWODIM/CHAR.”;
ONEDIM:= 24;
TWODIM:= 6;
DISC1:= 2;
DISC2:= 7;
REPLACE ALGOLARRAY [0] BY “ONETWOONETWOONETWOONETWO”;
CALL OUTSIDE (ALGOLARRAY, ONEDIM, TWODIM, DISC1, DISC2) [T];
END.
{PASCAL PROGRAM }
program pascal_twodim_schema((formalschema: formalschematype));
TYPE
indexrange = 1..10;
formalschematype(disc1, disc2:indexrange) =
packed array [disc1..5, 2..disc2] of char;
VAR
indexschema, indexschema2: indexrange;
BEGIN
for indexschema:= formalschema.disc1 to 5 do
for indexschema2:= 2 to formalschema.disc2 do
formalschema[ indexschema, indexschema2 ]:= 'a';
END.
