I have the following module with an allocatable variable which is defined in the module, allocated in a subroutine, and then also used in a second subroutine called by the first subroutine. In this situation do I have to pass the variable to the second subroutine and declare INTENT(inout)
? Or since it's a global variable it doesn't need to be passed as an argument?
MODULE test
IMPLICIT NONE
SAVE
REAL,ALLOCATABLE,DIMENSION(:,:,:) :: total
CONTAINS
!--- 1st subroutine
SUBROUTINE my_subr1(n,m,z)
IMPLICIT NONE
INTEGER,INTENT(in) :: n,m,z
ALLOCATE(total (n,m,z))
total=.9
CALL my_subr2(n)
END SUBROUTINE my_subr1
!-- 2nd subroutine
SUBROUTINE my_subr2(n)
IMPLICIT NONE
INTEGER,INTENT(in) :: n
total(n,:,:)=total(n-1,:,:)
END SUBROUTINE my_subr2
END MODULE test
do I have to pass the variable to the second subroutine and declare
INTENT(inout)
?
No, you don't. Any variable decalred in the body of the module has the save
attribute by default. You must, though, ensure that the second subroutine is only called after the first was executed, or else the program will fail because total
would not be initialized yet.
All functions and subroutines declared in the module will have access to total
by host association.
By the way, there are some issues you should address in your code, as mentioned by @PierredeBuyl in the comments:
SAVE
statement.IMPLICIT
directive from the module scope, there is no need to redeclare it in the subroutine if you won't change it.my_subr1
.