0

I am trying to pass an array of unspecified size to a subroutine like so

PROGRAM GOL
IMPLICIT NONE

INTEGER, PARAMETER :: size_x = 16, size_y = 16
LOGICAL, DIMENSION(1:size_x,1:size_y) :: universe
universe(:,:) = .FALSE.

CALL COUNT_NEIGHBOURS(universe, 1, 1)

END PROGRAM GOL

SUBROUTINE COUNT_NEIGHBOURS (universe, x, y)
LOGICAL, DIMENSION(:,:) :: universe
INTEGER :: x,y

!test
universe(x,y) = .TRUE.

RETURN
END SUBROUTINE COUNT_NEIGHBOURS

However I get the error from gfortran

CALL COUNT_NEIGHBOURS(universe, 1, 1)
                     1
Error: Procedure 'count_neighbours' at (1) with assumed-shape dummy argument 'universe' must have an explicit interface

What is the correct way to do this?

francescalus
  • 30,576
  • 16
  • 61
  • 96

1 Answers1

0

As described in the error message the compiler needs the explicit interface for your callable procedure. This answer shows three ways how to provide an interface.

In most cases the preferred way to put the procedure in a module or make it a contained procedure, but sometimes it's easier to use the interface block (in the code below) when, for example, you need to update some old code.

PROGRAM GOL
IMPLICIT NONE

interface
    subroutine COUNT_NEIGHBOURS(universe, x, y)
        logical :: universe(:,:)
        integer :: x, y
    end subroutine COUNT_NEIGHBOURS
end interface


INTEGER, PARAMETER :: size_x = 16, size_y = 16
LOGICAL, DIMENSION(1:size_x,1:size_y) :: universe
universe(:,:) = .FALSE.

CALL COUNT_NEIGHBOURS(universe, 1, 1)

END PROGRAM GOL

SUBROUTINE COUNT_NEIGHBOURS (universe, x, y)
LOGICAL :: universe(:,:)
INTEGER :: x,y

!test
universe(x,y) = .TRUE.

RETURN
END SUBROUTINE COUNT_NEIGHBOURS
IgorM
  • 307
  • 4
  • 7
  • 1
    As noted in [this answer](https://stackoverflow.com/a/42767455/3157076) to the question linked in comment above, there are other ways to provide the required explicit interface. This solution is, rightly, the least desirable, unless you can explain why it should be preferred? – francescalus Nov 23 '17 at 14:15
  • Modules are *much* better. Or make it internal for such a small example. – Vladimir F Героям слава Nov 23 '17 at 16:29