调用函数或子程序

Calling a function or subroutine

我是 Fortran 的新手,我正在尝试执行 function/subroutine 但出现错误 Explicit interface required

这是我的代码:

function printmat(m)
    integer, dimension(:,:) :: m
    integer :: row,col
    row = size(m,1)
    col = size(m,2)
       do k=1,row
            print *, m(k,1:col)
       enddo
end function printmat

program test
    integer, dimension(5, 5) :: mat
    integer :: i,j
    do i=1,5
    do j=1,5
           mat(j,i) = real(i)/real(j)
    enddo
    enddo
    call printmat(mat)
end program test

但是当我执行它时我得到:

Error: Explicit interface required for 'printmat' at (1): assumed-shape argument

知道它会是什么吗?我尝试将它包装到一个模块中,但是当我在程序中使用 "use modulename" 时它给了我一个错误(尝试从同名文件中读取它)

将其包装成一个模块,如果您想将其与 CALL 一起使用,则将其设为 subroutine

module printmat_module
contains
  subroutine printmat(m)
    integer, dimension(:,:) :: m
    integer :: row,col
    row = size(m,1)
    col = size(m,2)
    do k=1,row
       print *, m(k,1:col)
    enddo
  end subroutine printmat
end module printmat_module

program test
  use printmat_module
  integer, dimension(5, 5) :: mat
  integer :: i,j
  do i=1,5
     do j=1,5
        mat(j,i) = real(i)/real(j)
     enddo
  enddo
  call printmat(mat)
end program test

或者你可以只做编译器告诉你的,并为 program.

添加一个显式接口
subroutine printmat(m)
  integer, dimension(:,:) :: m
  integer :: row,col
  row = size(m,1)
  col = size(m,2)
  do k=1,row
     print *, m(k,1:col)
  enddo
end subroutine printmat

program test
  interface
     subroutine printmat(m)
       integer, dimension(:,:) :: m
     end subroutine printmat
  end interface
  integer, dimension(5, 5) :: mat
  integer :: i,j
  do i=1,5
     do j=1,5
        mat(j,i) = real(i)/real(j)
     enddo
  enddo
  call printmat(mat)
end program test