Like this sparge. 😄
The syntax for defining a variable and the return type of a function is the same (in Fortran 77, and therefore in Fortran 9x too). You think you are declaring a variable as character3 but you could equally be declariing a function of character3.
Have a look at the code below.
I moved your module to the head of the file (just so I could put it all in one file), but I have not changed anything in it. These three routines are valid Fortran code (if they are very ugly and contrived).
So, if the module is valid here, it must be valid in your example too. This must be the case since we require that the module should be capable of being compiled separately, without any knowledge as to how it is to be used, provided the interface is followed.
! Nothing has changed in this module
module knots
integer, parameter :: nb = 3
contains
character (len = nb) function chaout (chainp)
character (len = nb) chainp
integer b, inp (nb)
do b = 1, nb
inp (b) = ichar (chainp (b)) ! the error is here
end do
chaout = char (inp (2)) // char (inp (3)) // char (inp (1))
end function chaout
end module knots
program strung_out
use knots
EXTERNAL chainp ! << ADDED. Note it only goes in here. Not in the function chaout
character (len = nb) chainp, chance ! << Changed to declare type of function chainp
chance = chaout (chainp) ! Pass the external function
print *, chance ! prints yzx
stop
end program strung_out
! EXTERNAL FUNCTION
character(len=3) function chainp(p)
integer p
character(len=3) :: chintz
chintz = 'xyz'
chainp = chintz(p:p)
end function chainp