4

I have a simple Fortran 95 program

include "sandboxlib.f95"
program sandbox
    implicit none
    write(*, *) 'abc'
end program

and a simple module containing a function

module sandboxlib

 integer, parameter :: dp = kind(1.d0)

contains
function cumsum(mat, m, n) result(c)
    implicit none
    real(dp), intent(in) :: mat
    integer, intent(in) :: m, n
    integer i, j
    real(dp), dimension(m, n) :: c

    c(:, 1) = 0.d0

    do i = 2, m
        do j = 1, n
            c(i, j) = c(i-1, j) + mat(i, j)
        end do
    end do
end function
end module

I compile sandbox.f95 with this command

/usr/bin/gfortran -O -std=gnu -Wfatal-errors -pedantic -Wall sandbox.f95 -o sandbox

which results in this error

sandboxlib.f95:6.23:
    Included at sandbox.f95:1:

    function cumsum(mat, m, n)
                       1
Error: PROCEDURE attribute conflicts with INTENT attribute in 'mat' at (1)

I looked around and found a few questions that discuss modules, functions, etc. or an error like this, but I can't figure out why this won't compile.

Community
  • 1
  • 1
Michael A
  • 4,391
  • 8
  • 34
  • 61

1 Answers1

11

Your mat is declared scalar

  real(dp), intent(in) :: mat

but you use it as an array

  c(i, j) = c(i-1, j) + mat(i, j)

and the compiler parses this as a function call and assumes mat() is a function. And functions cannot have intent.

I assume the correct thing to do is to make mat an array in the declaration. Something like

  real(dp), intent(in) :: mat(:,:)

or

  real(dp), intent(in) :: mat(m,n) 

With the former you can avoid passing m and n as arguments.

  • Ah, thanks for the help. `mat(m, n)` fixes the error. Related question to what you said, though: if I use `mat(:, :)` instead, and therefore don't pass in `m` and `n`, I'll still need to calculate the dimensions inside the function, right? Otherwise the `do` loop won't work? – Michael A Apr 07 '17 at 23:17
  • @MichaelA You are right, you have to. Check `size()` and `lbound()` and `ubound()`. Sometimes the sizes can be global (module) variables, where it makes sense to share it this way. – Vladimir F Героям слава Apr 08 '17 at 07:17