What is the difference between stop
and exit
in Fortran?
Both can terminate the program immediately with some error information.
STOP tells the computer to stop running the program. In principle STOP can appear anywhere in the program and there can be any number of them. END tells the compiler (i.e. the program that takes your FORTRAN source and converts it into machine code) that this is the end of a program unit for compilation.
The STOP statement terminates execution of the program.
The return statement can be used to exit function and subroutine. Unlike many other programming languages it is not used to set the return value. This function performs an iterative computation. If the value of f becomes negative the function returns value -1000.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With