title: How to create function inside a Fortran subroutine?
body: <p>When I compile my code, the result is strange (very large as -1.112E+09). When I calculate by Matlab, the answer is -0.0873.</p>
  
  <pre><code>program test
  real, parameter :: X11=10,X22=5,X33=5,BE1A1=2,BE1B1=2
  real :: Y
  
  call J(X11,X22,X33,BE1A1,BE1B1,M)
  Y = M
  print *,Y
  
  end program test
  
    subroutine J(X1,X2,X3,BE1A1,BE1B1,M)
  
      real, intent(in) :: X1,X2,X3,BE1A1,BE1B1
      real, intent(out) :: M
  
      M = J1(X1,X2,X3,BE1A1,BE1B1)-J1(X1,X2,X3,BE1A1,-BE1B1)-J1(X1,X2,X3,-BE1A1,BE1B1)+J1(X1,X2,X3,-BE1A1,-BE1B1)
  
      contains
  
      real function J1(X1,X2,X3,EPS1,EPS2)
  
      real, intent(in) :: X1,X2,X3,EPS1,EPS2
      real :: R
      R = sqrt((X1-EPS1)**2+(X2-EPS2)**2+(X3)**2)
      J1 = log(R+X2-EPS2)
  
      end function J1
  
    end subroutine J
  </code></pre>