! --------------------------------------------- ! Example program ! subroutine called with a function as argument ! --------------------------------------------- program test implicit none real(kind(1.d0)):: pi,res,step,a,b intrinsic:: dsin ! a double precision version on sin() pi = 4.d0*atan(1.d0) a = 0.d0 b = pi step = 0.1d0 do call integral(dsin,a,b,step,res) write(*,'("step ",es8.2," integral ",es25.15)') step,res step = 0.1d0*step if(step<1.d-7) exit end do end program test ! ------------------------------ ! computes the definite integral ! of f(x) from a to b with step dx ! as a Riemann sum ! ------------------------------ subroutine integral(f,a,b,dx,res) implicit none real(kind(1.d0)), intent(in):: a,b,dx real(kind(1.d0)), intent(out):: res real(kind(1.d0)), external:: f real(kind(1.d0)):: x res = 0.d0 x = a do res = res + f(x) x = x + dx if(x>b) exit end do res = dx*res end subroutine integral