Вопрос

I have a long conditional statement to determine whether to exit a do while loop that I use in several places.

if (long_class_name <= something + 0.5 .and. &
    long_class_name >= something - 0.5 .and. &
    long_class_name <= some_other_thing + 0.5 .and. &
    long_class_name >= some_other_thing - 0.5 .and. &
    yet_another_thing == -999.0) exit

Is there a way return an exit statement via a subroutine or function in Fortran, like:

call check_for_exit

It would be nice to not have to add variables, etc. for doing so (e.g. return a logical from a function, that I then do a simpler if on):

exit_now = check_exit()
if (exit_now == .true.) exit
Это было полезно?

Решение

Why not simply use a logical function like in your example and directly test it (without an additional variable):

if ( check_exit() ) exit

Or, if you want to use integers, e.g. negative integers denote errors and positive ones are warnings:

if ( complicated_check() > 0 ) then
  print *, 'WARNING'
elseif( complicated_check() < 0 ) then
  exit
endif

Although this is starting to get inefficient without auxiliary variables ;-)

Другие советы

if you want to handle multiple return values, assuming f90+ you can use select case to avoid the auxiliary variable (or calling the function twice)

 select case ( complicated_check() )
  case (1:)
   print*,'Warning'
  case (:-1)
   exit
 end select
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top