fortran - confused about the intent of a function passed to a subroutine -
in doing following integral don't understand shouldn't intent of a,b in intent of f? shouldn't argument variables in subroutine have intent? confusing me though works know how?
also, see x intent (in) since y gets passed formal argument of f in subroutine. i'm not sure going on intents of subroutine seems integral should intent(out) right?
program main implicit none real*10 :: integral a= 1; b =2;n=1000; call simpson(f,a,b,integral,n) real*10 function f(x) real*10, intent(in) :: x f = x**(2.*charge)*exp(-a*x**2 -(b/2)*x**4) end function f subroutine simpson(f,a,b,integral,n) real*10 :: integral, a, b real*10 :: f real*10 h, y ,s integer n, ! if n odd add +1 make if((n/2)*2.ne.n) n=n+1 ! loop on n (number of intervals) s = 0.0 h = (b-a)/dfloat(n) i=2, n-2, 2 y = a+dfloat(i)*h s = s + 2.0*f(y) + 4.0*f(y+h) end integral = (s + f(a) + f(b) + 4.0*f(a+h))*h/3.0 end subroutine simpson end program main
if not defined, fortran compilers recognize variables having intent
of inout
.
procedure arguments, such f
, different , not in
, out
, or inout
. if specify f
either of those, error message says along lines of
error: procedure attribute conflicts intent attribute in 'f' @ (1)
Comments
Post a Comment