How to stop a subroutine and raise a flag?

半世苍凉 提交于 2019-12-11 04:19:55

问题


I am writing a program in Fortran 95 (to be compiled with with gfortran) containing a subroutine that performs a certain computation. As suggested in "Fortran 95/2003 for Scientists & Engineers" by S. J. Chapman, I am trying to stop the subroutine when an error is encountered and "throw"[1] an error flag that is "catch"ed[1] by the calling program, that will take all the necessary actions. Ideally, I am going for something like:

! Pseudo-code
PROGRAM my_prog
    integer :: error_flag
    CALL my_subr (<input_args>, <output_args>, error_flag)
    ! Also error_flag is an output: 0 -> everything OK, 1 -> error
    IF (error_flag /= 0) THEN
        WRITE (*,*) 'Error during execution of "my_subr"'
    ELSE
        ... do something ...
    END IF
END PROGRAM my_prog

How can I stop the subroutine and gracefully handle the errors?

Here is an example: the subroutine "division" takes an integer input value and iteratively divides it by a value that is the input value decremented by the number of steps-1. When such value reaches zero, a flag should be raised and the subroutine should be exited without performing the division by zero.

SUBROUTINE division (inval, outval, error_flag)
  IMPLICIT NONE

  INTEGER, INTENT(IN) :: inval
  REAL, INTENT(OUT) :: outval
  INTEGER, INTENT(OUT) :: error_flag ! 0 -> OK, 1 -> error

  INTEGER :: i
  REAL :: x

  error_flag = 0
  x = REAL(inval)
  DO i = 0, 10
     IF (inval-i == 0) error_flag = 1
     ! How can I gracefully exit now?
     x = x / REAL(inval-i)
  END DO

END SUBROUTINE division

PROGRAM my_prog
  IMPLICIT NONE
  REAL :: outval
  INTEGER :: error_flag
  CALL division (8, outval, error_flag)
  IF (error_flag == 1) THEN
     WRITE (*,*) 'Division by zero'
  ELSE
     WRITE (*,*) 'Output value:', outval
  END IF
END PROGRAM my_prog

Notes:

[1] I am borrowing (in a probably inappropriate way) C++'s jargon.


回答1:


Seeing your example it seems that you are just missing the return statement:

  error_flag = 0
  x = REAL(inval)
  DO i = 0, 10
     IF (inval-i == 0) then
                         error_flag = 1
                         return
     END IF

     x = x / REAL(inval-i)
  END DO



回答2:


One possibility would be to change

  DO i = 0, 10
     IF (inval-i == 0) error_flag = 1
     ! How can I gracefully exit now?
     x = x / REAL(inval-i)
  END DO

to

  DO i = 0, 10
     IF (inval-i == 0) THEN 
        error_flag = 1
        EXIT
     END IF
     ! Now you have gracefully exited
     x = x / REAL(inval-i)
  END DO
  ! Code to tidy up if the error flag was set

Here the EXIT statement exits the loop -- Vladimir's answer shows you how to use RETURN to exit the subroutine more immediately. Whichever approach you choose don't forget to assign to outval before leaving the subroutine.



来源:https://stackoverflow.com/questions/31873986/how-to-stop-a-subroutine-and-raise-a-flag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!