Sam Zipper
Sam Zipper

Reputation: 652

Value of integer changes between call and subroutine

I'm trying to implement a new subroutine, inigw, in a decades-old Fortran-based program, and struggling mightily. I'm a total Fortran beginner so hopefully I'm missing something obvious.

I am calling a subroutine, inigw(iyear,imonth,iday,nday,time,seed). Of the 6 input variables to the call, all are integers except time. If I write the variables out immediately before calling, everything looks fine:

 if (gwexist.eq.2 .and. iyear.ge.igwyear .and. iyear.le.igwend) then
            write(*,*) 'iyear in call = ', iyear
            write(*,*) 'imonth in call = ', imonth
            write(*,*) 'iday in call = ', iday
            write(*,*) 'nday in call = ', nday
            write(*,*) 'time in call = ', time
            write(*,*) 'seed in call = ', seed
    call inigw (iyear,imonth,iday,nday,time,seed)  
 endif

The output looks as I would expect:

 iyear in call =         2013
 imonth in call =            1
 iday in call =            4
 nday in call =            4
 time in call =    28800.00    
 seed in call =   1776642162

However, the values of all the integers (iyear, imonth, iday, nday, and seed) are changed within my subroutine.

c ---------------------------------------------------------------------
      subroutine inigw (iyear, imonth, iday, nday, time, seed)
c ---------------------------------------------------------------------
c SCZ- 1/31/2014
c This routine is intended to read in hourly depth to groundwater data from 
c text files. Much of the language is borrowed from inimet.               

c Define some variables   
          implicit none

      real 
     >  dtgwin(ndpts),             ! read-in depth to groundwater
     >  time,                      ! time in seconds since start of day
     >  tdomthick                  ! total domain thickness [m]

     integer
     >  seed,
     >  nday,
     >  iyear,                     ! main routine year
     >  imonth,                    ! main routine month
     >  iday,                      ! main routine day
c
      write(*,*) 'iyear in inigw = ', iyear
      write(*,*) 'imonth in inigw = ', imonth
      write(*,*) 'iday in inigw = ', iday
      write(*,*) 'nday in inigw = ', nday
      write(*,*) 'time in inigw = ', time
      write(*,*) 'seed in inigw = ', seed
...

The values of the integers are now different, but the real number (time) is the same!

 iyear in inigw =   2.8208138E-42
 imonth in inigw =   1.4012985E-45
 iday in inigw =   5.6051939E-45
 nday in inigw =   5.6051939E-45
 time in inigw =    28800.00    
 seed in inigw =   3.4667155E+25

What's interesting is that there's another subroutine, inimet, structured in basically the same way with the same inputs to the call, and the variables pass through that call successfully. I assume I'm missing something obvious here, but I'm not sure exactly what. I've tried Googling and found nothing. Sorry I can't provide a reproducible example- I don't know enough about Fortran to write one.

Upvotes: 0

Views: 215

Answers (2)

francescalus
francescalus

Reputation: 32366

As we've established that the problem was in indentation (see the answer by @paxdiablo) I suppose it won't hurt to elaborate on why.

The code is using fixed-form source code. This means that a character other than a blank or "0" in column 6 is treated as a continuation character. Your "i" of "integer" is such a character. This, coupled with the general insignificance of blanks, means that your code snippet

      real 
     >  dtgwin(ndpts),             ! read-in depth to groundwater
     >  time,                      ! time in seconds since start of day
     >  tdomthick                  ! total domain thickness [m]

     integer
     >  seed,
     >  nday,

is equivalent to

      real 
     >  dtgwin(ndpts),             ! read-in depth to groundwater
     >  time,                      ! time in seconds since start of day
     >  tdomthickntegerseed,
     >  nday,

(also noting that comments do not affect program interpretation).

So nday and the following variables are indeed declared real.

Note that there is no variable seed or tdomthick declared explicitly, so your implicit none confuses me as to why your code compiled. [Even dummy arguments need to be explicitly declared with implicit none unless you're using some other form of association to get these variables?]

Finally, I'll echo @M.S.B.'s comment about not writing new code in fixed-form, as I imagine you aren't using an F77-only compiler.

Upvotes: 1

paxdiablo
paxdiablo

Reputation: 881383

Strangely enough, time is the only one that appears to remain unchanged. It's also the only one that's not declared in the function with an integer statement that appears to begin one column too far to the left.

Fix the indentation of that and your problem should be solved. FORTRAN was one of those languages that was very picky about this sort of thing.

Upvotes: 1

Related Questions