What is the difference between stop
and exit
in Fortran?
Both can terminate the program immediately with some error information.
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 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.
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