Error: DUMMY attribute conflicts with RESULT attribute

Error: DUMMY attribute conflicts with RESULT attribute

我有一个按以下方式格式化的 Fortran 90 程序

program main
<bulk of code that uses update_board>
end program main

integer function update_board(board, tochange, N) result(tochange)
    integer, dimension(:, :) :: board, tochange
    integer N
    
    do i=2,N+1
        do j=2,(N+2)/2
            nna = board(i+1, j+1) + board(i, j+1) + board(i-1, j+1) &
            + board(i-1, j) + board(i-1, j-1) + board(i, j-1) &
            + board(i+1, j-1) + board(i+1,j)
            
            if (nna .eq. 3) then
                tochange(i, j) = 1
            else if (nna .eq. 2) then
                tochange(i, j) = board(i, j) 
            else
                tochange(i, j) = 0
            end if
        end do
    end do
end function update_board

然而,当我运行代码时,我得到

的编译错误
integer function update_board(board, tochange, N) result(tochange)
                                                                  1
Error: DUMMY attribute conflicts with RESULT attribute in 'tochange' at (1)
parallel_game_of_life.f90:1.12:

program main
            1
parallel_game_of_life.f90:144.47:

    integer, dimension(:, :) :: board, tochange
                                               2
Error: Two main PROGRAMs at (1) and (2)

我确定后一个 two main PROGRAMs 错误是第一个错误的直接结果。但是,我很困在这里。我想更新一个输入变量并将其用作结果,因为我不能声明像 integer, dimension(:, :) function ...

这样的函数

您在代码中声明的函数输出不正确。您已两次声明输出类型,但同时将其作为输入和输出。如果你想把它作为输入,那么使用 subroutine 而不是 function。根据我的理解,这是对您的代码的修复,

function update_board(board, n) result(tochange)
    integer, intent(in) :: n
    integer, intent(in) :: board(n,(n+2)/2+1)
    integer             :: tochange(n,(n+2)/2+1)
    
    do i=2,N+1
        do j=2,(N+2)/2
            nna = board(i+1, j+1) + board(i, j+1) + board(i-1, j+1) &
            + board(i-1, j) + board(i-1, j-1) + board(i, j-1) &
            + board(i+1, j-1) + board(i+1,j)
            
            if (nna .eq. 3) then
                tochange(i, j) = 1
            else if (nna .eq. 2) then
                tochange(i, j) = board(i, j) 
            else
                tochange(i, j) = 0
            end if
        end do
    end do
end function update_board

等效的 subroutine 实现如下,

subroutine update_board(n, board, tochange)
    integer, intent(in)  :: n
    integer, intent(in)  :: board(n,(n+2)/2+1)
    integer, intent(out) :: tochange(n,(n+2)/2+1)
    
    do i=2,N+1
        do j=2,(N+2)/2
            nna = board(i+1, j+1) + board(i, j+1) + board(i-1, j+1) &
            + board(i-1, j) + board(i-1, j-1) + board(i, j-1) &
            + board(i+1, j-1) + board(i+1,j)
            
            if (nna .eq. 3) then
                tochange(i, j) = 1
            else if (nna .eq. 2) then
                tochange(i, j) = board(i, j) 
            else
                tochange(i, j) = 0
            end if
        end do
    end do
end function update_board

然后,call subroutine(n, board, tochange) 而不是 tochange = function(board, n)