'Why 2 "END MODULE" statements? [duplicate]

I found some code for an array and I was testing it out when I ran into an issue. Here is the code-

program arrayToProcedure      
implicit none      

   integer, dimension (5) :: myArray  
   integer :: i
   
   call fillArray (myArray)      
   call printArray(myArray)
   
end program arrayToProcedure

module myArray
implicit none 

subroutine fillArray (a)            
   integer, dimension (5):: a
   
   ! local variables     
   integer :: i     
   do i = 1, 100         
      a(i) = i      
   end do  
   
end subroutine fillArray 
subroutine printArray(a)

   integer, dimension (5) :: a  
   integer::i
   
   do i = 1, 100
      Print *, a(i)
   end do
   
end subroutine printArray
end module myArray

and here is the error message Error: Expecting END MODULE statement at (1)

I already have an END MODULE statement! What's going on?



Solution 1:[1]

It looks like there are several issues:

  • The module is missing a CONTAINS statement before the definitions of the subroutines in the module.
  • The module is defined after the main program. (This might be possible, but I moved the program to the end to get the file to compile.)
  • The functions access a(i) with i ranging from 1 to 100, but the dimension of a is 5.
  • The main program has variables with the same name as the module.
  • The main program doesn't have a USE statement for the module.

Here's an edited version of your code:

module myArray
implicit none 

contains

subroutine fillArray (a)            
   integer, dimension (5):: a
   
   ! local variables     
   integer :: i     
   do i = 1, 5        
      a(i) = i      
   end do  
   
end subroutine fillArray 
subroutine printArray(a)

   integer, dimension (5) :: a  
   integer::i
   
   do i = 1, 5
      Print *, a(i)
   end do
   
end subroutine printArray

end module myArray


program arrayToProcedure      
use myArray
implicit none      

   integer, dimension (5) :: arr  
   integer :: i
   
   call fillArray (arr)      
   call printArray(arr)
   
end program arrayToProcedure

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Warren Weckesser