Search code examples
arraysfortrandeclare

How does dimension work in Fortran


I don't get why

IMPLICIT REAL*8(A-Z)
DIMENSION A(20)

compiles fine, and

REAL*8, DIMENSION A(20)

results in error

Missing dimension specification at ...


Solution

  • Those are two different meanings of dimension. The first is a dimension statement and the second you mean to specify the dimension attribute in a declaration.

    In a declaration where attributes are specified it is necessary to have :::

    REAL*8, DIMENSION(20) :: A
    

    Note also that the array specification comes attached to the dimension, not to the variable name.

    Use of :: is merely optional in a dimension statement (except in Fortran 77 where it wasn't allowed).

    However, it is simply allowed to write

    real*8 A(20)
    

    as the dimension attribute is also specified by giving the array specification.


    It's possibly also worth noting that, as the declaration line in the question is incorrect, that in fixed-form source the mistake is different.

    In fixed-form source, spaces are not of note (beyond the column layout), so

          real*8, dimension a(20)
    

    is the statement

          real*8 dimensiona(20)
    

    with an extraneous comma.