In the following code example the compiler indicates a name conflict in example 1 but not in example 2. Is this expected behaviour? What effect might it have on code compiled in similar style to example 2 regarding the actual variable A? Should the global variable A be updated even though a local variable will the same name is defined? In other words if the user mistakenly uses a variable name that also exists in the module as global variable this global variable may be modified (wrongly) and be difficult to track down in the debugger which will display the local value unless testlib::A is entered in a watch window.
module testlib implicit none integer :: A = 1 end module module ex1 contains subroutine Ex1Sub(v) ! Declare Module inside contained procedure use testlib implicit none integer,intent(out) :: v integer :: A ! Compiler flags name conflict integer :: i A = 0 do i = 1 , 4 A = A + 1 enddo v = A end subroutine end module module ex2 ! Declare Module Above contained procedures use testlib contains subroutine Ex2Sub(v) implicit none integer,intent(out) :: v integer :: A ! Compiler does NOT flag up name conflict integer :: i A = 0 do i = 1 , 4 A = A + 1 enddo v = A end subroutine end module
I prefer to declare all modules at the top of a higher level module rather than individually in each internal procedure but this may lead on to bugs that are hard to detect as debugger may not shown the contents of the correct variable and the global module variable may get modified within a procedure by mistake. Can anybody explain what is really happening here and what is the best practice to adopt.