Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between "stop" and "exit" in Fortran?

Tags:

What is the difference between stop and exit in Fortran?

Both can terminate the program immediately with some error information.

like image 523
Taitai Avatar asked Dec 26 '15 22:12

Taitai


People also ask

What is difference between stop and end in Fortran?

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.

What is stop in Fortran?

The STOP statement terminates execution of the program.

How do you exit a function in Fortran?

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.

How do you exit a subroutine in Fortran?

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.


2 Answers

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.

like image 98
francescalus Avatar answered Sep 17 '22 13:09

francescalus


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

like image 31
Craig Estey Avatar answered Sep 19 '22 13:09

Craig Estey