Passing an array of undefined size to a subroutine [duplicate]

爷,独闯天下 提交于 2019-12-13 03:19:07

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/47455067/passing-an-array-of-undefined-size-to-a-subroutine

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!