1

I wrote a subroutine for a Fortran program and I want to execute a command (delete file in the program directory and open a new one) the first, and only first time, the subroutine is called. I know that I can achieve this by creating some flag outside of the subroutine, in the main program, that I set to false at program startup and then set to true upon entering the subroutine. Then I could use this flag in an if statement to figure if the commands I want to execute on the initial call should be executed or not. But this requires me modifying the existing program and I didn't want to do that if I could avoid it. Is there some other way to do what I want to do?

rks171
  • 1,000
  • 11
  • 26
  • 2
    Look [here](http://stackoverflow.com/questions/2582409/are-local-variables-in-fortran-77-static-or-stack-dynamic) for the `save` attribute to make variables static. – ceving Oct 12 '12 at 15:23
  • Yes, I'm aware of the 'save' attribute, but I'm not sure how I would use it for this case. – rks171 Oct 12 '12 at 15:30
  • 2
    Using a static variable keeps the modification local to your function. A first call can clear the flag and succeeding calls can honor it. – ceving Oct 12 '12 at 15:38
  • How would the subroutine know that it is a first call and to subsequently clear the flag? – rks171 Oct 12 '12 at 15:40
  • Initially it has a default value for example 0. And after that you increment it in every call. Than you can check if it is different from 0. – ceving Oct 12 '12 at 15:42
  • So, I would need to set the flag as zero outside of the subroutine, when the program starts, for example. I wanted to see if I could do this without any modification to the code except for the call to my new subroutine. – rks171 Oct 12 '12 at 15:45
  • Maybe I could compile my subroutine with a flag to initialize all variables to zero, so the flag will be guaranteed zero during the first call, and then non-zero afterwards? Is there such a flag for ifort? – rks171 Oct 12 '12 at 16:27
  • 1
    @rks171 - No. You have the variable declared as, for instance, `logical, save :: firstcall = .TRUE.` and then have the initialization code do `if (firstcall) then ... stuff .. firstcall = .FALSE. ; endif`. Note that there's real downsides (threadsafety, eg) to using static variables, and a nicer way to do it would just be to have the calling function pass a flag to the routine to modify its behaviour the first time. – Jonathan Dursi Oct 12 '12 at 17:00
  • 1
    See this example: http://stackoverflow.com/questions/2893097/fortran-save-statement/2893604#2893604 . Inside of the If statement do your actions that you only want to perform on the first call. – M. S. B. Oct 12 '12 at 19:11

2 Answers2

4

An example might be:

subroutine test(a)
  implicit none
  integer, intent(inout) :: a
  logical, save :: first_time=.true.

  if(first_time) then
     first_time=.false.
     a = a + 12345
  else
     a = a - 67890
  end if

end subroutine test
weymouth
  • 521
  • 6
  • 17
  • 1
    Local variables with initialisers are implicitly saved, thus the explicit `save` attribute is redundant. – Hristo Iliev Oct 13 '12 at 15:13
  • 2
    But for safety and clarity, why not add it? – weymouth Oct 13 '12 at 23:32
  • I understand now. I didn't realize that the declaration of first_time as .true. is only honored the first time the subroutine is entered. I thought that every time that 'test' was entered, it would just keep setting first_time equal to .true. again. Now I can see that the saved value is honored upon re-entry into 'test'. Thanks. – rks171 Oct 15 '12 at 19:28
1

How about using some characteristic of the output file to determine whether or not to delete it? Time stamp, file lock, a particular file extension, etc.

WaywiserTundish
  • 122
  • 1
  • 3