26

What is the difference between stop and exit in Fortran?

Both can terminate the program immediately with some error information.

nbro
  • 15,395
  • 32
  • 113
  • 196
Taitai
  • 584
  • 2
  • 7
  • 18

2 Answers2

25

exit in Fortran is a statement which terminates loops or completes execution of other constructs. However, the question is clearly about the non-standard extension, as either a function or subroutine, offered by many compilers which is closely related to the stop statement.

For example, gfortran offers such a thing.

As this use of exit is non-standard you should refer to a particular implementation's documentation as to what form this takes and what effects it has.

The stop statement, on the other hand, is a standard Fortran statement. This statement initiates normal termination of execution of a Fortran program (and can be compared with the error stop statement which initiates error termination).

Other than knowing that terminating (normally) execution of the program follows a stop statement and that there is a stop code, the actual way that happens are again left open to the implementation. There are some recommendations (but these are only recommendations) as to what happens. For example, in Fortran 2008 it is suggested that

  • the stop code (which may be an integer or a string) be printed to an "error" unit;
  • if the stop code is an integer it be used as the process exit status;
  • if there is no stop code (or it's a character) zero be returned as the exit status.

The above is fairly vague as in many settings the above concepts don't apply.

Typically in practice, exit will be similar to the C library's function of that name and its effect will be like stop without a stop code (but still passing the given status back to the OS).

In summary, Fortran doesn't describe a difference between stop and exit. Use of exit (for termination) is non-portable and even the effect of stop is not entirely defined.

francescalus
  • 30,576
  • 16
  • 61
  • 96
11

stop is a fortran statement but exit is a function that just happens to terminate the program.

The stop statement will output its argument [which can also be a string] to stderr

stop 123

and it will return a zero status to the parent process.

Whereas exit is a function and must be called like any other. It will also be silent (i.e. no message):

call exit(123)

and the argument to exit will be returned to the parent process as status

Craig Estey
  • 30,627
  • 4
  • 24
  • 48