Passing 2D array of unknown size to function in Fortran

Trying to input a 2D array with an unknown size, n, in one of its dimensions as the input to a function. So, the input will be (n, 3) and output (n, 1). These will represent n Cartesian coordinates.

So far have been trying:

function IterativeHarmonics(l, m, x)
   real :: IterativeHarmonics
   integer, intent(in) :: l, m
   real, intent(in) :: x(:,3)

   IterativeHarmonics = x(1,3)
end function IterativeHarmonics

But this gives the errors

Error: Bad specification for deferred shape array at (1)


Error: Function ‘x’ at (1) has no IMPLICIT type


  • As with deferred-shape arrays, we cannot just partially assume the shape of an array.

    That is,

    real x(:,3)

    is not allowed, even where we know the extent of the second dimension will always be 3. We must assume like

    real x(:,:)

    If you want to use assumed-shape arrays, but insist on the second extent being of a specific value, you'll need to add your own checks. You can, of course, use an explicit shape where the first extent is passed:

    real x(n,3)   ! n accessible somehow, probably as dummy

    (The error about x being a function and of implicit type follows on from the faulty declaration.)