1

In doing the following integral I don't understand shouldn't the intent of a,b be IN what is the intent of f? shouldn't all argument variables in a subroutine have an intent? This is confusing for me even though it works i Would like to know how?

Also, I see that x is intent (in) since y gets passed as the formal argument of f in the subroutine. I'm just not sure what is going on with the intents of the subroutine also it seems like integral should be 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, i
    ! if n is odd we add +1 to make it even
    if((n/2)*2.ne.n) n=n+1
    ! loop over n (number of intervals)
    s = 0.0
    h = (b-a)/dfloat(n)
    do i=2, n-2, 2
        y   = a+dfloat(i)*h
    s = s + 2.0*f(y) + 4.0*f(y+h)
    end do
    integral = (s + f(a) + f(b) + 4.0*f(a+h))*h/3.0 
    end subroutine simpson
        end program main
Timtam
  • 223
  • 2
  • 8

1 Answers1

2

If not defined, Fortran compilers recognize all variables as having intent of inout.

Procedure arguments, such as your f, are different and are not really in, out, or inout. If you do specify f as either of those, you will get an error message that says something along the lines of

  Error: PROCEDURE attribute conflicts with INTENT attribute in 'f' at (1)
Kyle Kanos
  • 3,257
  • 2
  • 23
  • 37
  • 1
    Arguments with omitted `intent` and `intent (inout)` are very similar but not absolutely identical ... see http://stackoverflow.com/questions/2880537/fortran-intentinout-versus-omitting-intent – M. S. B. Jul 18 '13 at 13:56
  • @M.S.B.: I see, thanks for the link. One of these days I might actually read Adams' book :) – Kyle Kanos Jul 18 '13 at 14:09