-1

i run into a strange problem. I am using the locate subroutine from the Fortran-Recipe book. The goal is to find a random number in a CDF-table and return the index of the arrax, such that i can look in another table for the value this would correspond. However i cannot ge it to pass the value of j (jlo in the encompassing function) back. It just stays 0. The subroutine locate changes the value of j to the correct one, but then does not pass it back.

      SUBROUTINE locate(xx,n,x,j)
      INTEGER, INTENT(out) :: j
      REAL*8 x,xx(n)
      INTEGER jl,jm,ju,n
      jl=0
      ju=n+1

10    if(ju-jl.gt.1)then
        jm=(ju+jl)/2
        if((xx(n).ge.xx(1)).eqv.(x.ge.xx(jm)))then
          jl=jm
        else
          ju=jm
        endif
      goto 10
      endif

      if(x.eq.xx(1))then
        j=1
      else if(x.eq.xx(n))then
        j=n-1
      else
        j=jl
      endif
      return
      END

This is the design of the subroutine

and this is how it is included:

function CDF() result(xyz)
implicit none
real(BW) :: rand
integer(I2B) :: jlo, N_points_table
real(BW), allocatable :: CDFtable(:)


N_points_table= 1000
jlo=0
CALL RANDOM_NUMBER(rand)

allocate(CDFtable(N_points_table))
...
!setting the content of CDFtable to a CDF of some kind
...
call locate(CDFtable,N_points_table,rand,jlo)
....
....

However after the subroutine ran, jlo is still 0 as it was initiated. Any help is greatly appreciated

busssard
  • 158
  • 1
  • 1
  • 11
  • Please show more code ([mcve]). How are the procedures connected? Are they in a module? Are they external? Is there an explicit interface available? Do not use `...`, show the real code. You can simplify it, but test the simplified code before posting it. Use all debugging features your compiler has (like `gfortran -g -Wall -fcheck=all`). – Vladimir F Героям слава Feb 20 '19 at 13:14
  • The dummy and actual arguments to the routine do not necessarily match in type, kind and rank. Use implicit none and get an interface in scope, then you would never have such errors. – Ian Bush Feb 20 '19 at 15:03
  • The problem was that the subroutine was in a different file. Once i included the subroutine in the same module as the function all worke well. – busssard Feb 20 '19 at 17:27

1 Answers1

0

The problem was that the subroutine was in a different file. Once i included the subroutine in the same module as the function all worke well.

busssard
  • 158
  • 1
  • 1
  • 11
  • As you describe it that should make no difference whatsoever, I strongly suspect you have another problem which rearrange the program in memory is now masking. You are using all the run time checks? – Ian Bush Feb 21 '19 at 08:09