Concepts of Programming Languages

(Sean Pound) #1
6.5 Array Types 261

appears in its own brackets. A problem with using parentheses
to enclose subscript expressions is that they often are also used
to enclose the parameters in subprogram calls; this use makes
references to arrays appear exactly like those calls. For example,
consider the following Ada assignment statement:

Sum := Sum + B(I);

Because parentheses are used for both subprogram parameters
and array subscripts in Ada, both program readers and compilers
are forced to use other information to determine whether B(I)
in this assignment is a function call or a reference to an array ele-
ment. This results in reduced readability.
The designers of Ada specifically chose parentheses to
enclose subscripts so there would be uniformity between array
references and function calls in expressions, in spite of potential
readability problems. They made this choice in part because both
array element references and function calls are mappings. Array
element references map the subscripts to a particular element of
the array. Function calls map the actual parameters to the func-
tion definition and, eventually, a functional value.
Most languages other than Fortran and Ada use brackets to
delimit their array indices.
Two distinct types are involved in an array type: the element type and the
type of the subscripts. The type of the subscripts is often a subrange of inte-
gers, but Ada allows any ordinal type to be used as subscripts, such as Boolean,
character, and enumeration. For example, in Ada one could have the following:

type Week_Day_Type is (Monday, Tuesday, Wednesday,
Thursday, Friday);
type Sales is array (Week_Day_Type) of Float;

An Ada for loop can use any ordinal type variable for its counter, as we
will see in Chapter 8. This allows arrays with ordinal type subscripts to be
conveniently processed.
Early programming languages did not specify that subscript ranges must
be implicitly checked. Range errors in subscripts are common in programs, so
requiring range checking is an important factor in the reliability of languages.
Many contemporary languages do not specify range checking of subscripts, but
Java, ML, and C# do. By default, Ada checks the range of all subscripts, but this
feature can be disabled by the programmer.
Subscripting in Perl is a bit unusual in that although the names of all arrays
begin with at signs (@), because array elements are always scalars and the names of
scalars always begin with dollar signs ($), references to array elements use dollar
signs rather than at signs in their names. For example, for the array @list, the
second element is referenced with $list[1].

History Note


Fortran I limited the number
of array subscripts to three,
because at the time of the
design, execution efficiency was
a primary concern. Fortran
I designers had developed a
very fast method for accessing
the elements of arrays of up
to three dimensions, using the
three index registers of the IBM



  1. Fortran IV was first imple-
    mented on an IBM 7094, which
    had seven index registers. This
    allowed Fortran IV’s designers
    to allow arrays with up to seven
    subscripts. Most other contem-
    porary languages enforce no
    such limits.

Free download pdf