question on intent(in) and changing the input inside a subroutine
- From: "Nasser M. Abbasi" <nma@xxxxxxxxx>
- Date: Sat, 23 Jun 2012 13:32:19 -0500
I was looking at this web page of best practices in Fortran
http://fortran90.org/src/best-practices.html#arrays
and it shows this code fragment:
-----------------------------
subroutine f(r)
real(dp), intent(in) :: r(:)
integer :: n, i
n = size(r)
do i = 1, n
r(i) = 1.0_dp / i**2
enddo
end subroutine
--------------------------
The first strange thing I noticed is that the formal
parameter r is declared as 'in' yet it is being written
to inside the subroutine. But based on Fortran standard:
"intent(in) means that the variable value can enter, but not be changed"
So, what is going on here?
When I tried it on my end, I am getting an error.
Here is my code to test the above. all in same one file.
------------ t6.f90 -------------------------
module inc
implicit none
contains
subroutine f(r)
integer, parameter:: dp=kind(0.d0)
real(dp), intent(in) :: r(:)
integer :: n, i
n = size(r)
do i = 1, n
r(i) = 1.0_dp / i**2
enddo
end subroutine f
end module inc
program main
use inc
implicit none
integer, parameter:: dp=kind(0.d0)
real(dp) :: r(5)
call f(r)
end program main
----------------------------
---------------------------
gfortran -fcheck=all -Wall t6.f90t6.f90:11.10:
r(i) = 1.0_dp / i**2
1
Error: Dummy argument 'r' with INTENT(IN) in variable definition context (assignment) at (1)
t6.f90:17.8:
use inc
1
Fatal Error: Can't open module file 'inc.mod' for reading at (1): No such file or directory
------------------------------
Am I doing something wrong? could the web page really be wrong?
Has the standard changed and once it was allowed to write
to an 'in' parameter? does not make much sense.
I am not good enough in Fortran to say that a Fortran90.org
'best practices' page is wrong ! But it sure looks like it.
thanks,
--Nasser
.
- Follow-Ups:
- Re: question on intent(in) and changing the input inside a subroutine
- From: Louisa
- Re: question on intent(in) and changing the input inside a subroutine
- From: Jos Bergervoet
- Re: question on intent(in) and changing the input inside a subroutine
- From: Richard Maine
- Re: question on intent(in) and changing the input inside a subroutine
- Prev by Date: Re: how to read parameter from file (i.e. to read integer, and then to use it as paarameter)?
- Next by Date: Re: question on intent(in) and changing the input inside a subroutine
- Previous by thread: how to read parameter from file (i.e. to read integer, and then to use it as paarameter)?
- Next by thread: Re: question on intent(in) and changing the input inside a subroutine
- Index(es):
Relevant Pages
|