Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fortran intent(inout) versus omitting intent

Good practice dictates that subroutine arguments in Fortran should each have a specified intent (i.e. intent(in), intent(out) or intent(inout) as described this question):

subroutine bar (a, b)     real, intent(in) :: a     real, intent(inout) :: b     b = b + a     ... 

However, not specifying an intent is valid Fortran:

subroutine bar (a, b)     real, intent(in) :: a     real :: b     b = b + a     ... 

Are there any real differences beyond compile time checking for an argument specified as intent(inout) and an argument without a specified intent? Is there anything I should worry about if I'm retrofitting intents to older, intent free, code?

like image 249
Andrew Walker Avatar asked May 21 '10 08:05

Andrew Walker


People also ask

What is Fortran INTENT out?

INTENT(OUT) specifies that the dummy argument must be defined before it is referenced within the subprogram. Such a dummy argument might not become undefined on invocation of the subprogram. INTENT(INOUT) specifies that the dummy argument can both receive and return data to the invoking subprogram.

How many argument can one INTENT have?

More precisely, in subroutine Means(), some values must be stored into these three arguments so that they can be passed out. Note that an argument declared with INTENT(OUT) does not have to receive any value from outside of the subroutine.

How do I use subroutine in Fortran?

A Fortran function is similar to a mathematical function, which takes one or many parameters as inputs and returns a single output value. A Fortran subroutine is a block of code that performs some operation on the input variables, and as a result of calling the subroutine, the input variables are modified.


1 Answers

According to The Fortran 2003 Handbook by Adams, et al., there is one difference between an intent(inout) argument and argument without specified intent. The actual argument (i.e., in the caller) in the intent(inout) case must always be definable. If the intent is not specified, the argument must be definable if execution of the subroutine attempts to define the dummy argument. definable means setting the value: dummy_arg = 2.0. Clearly the actual argument should be a variable if this is done. For intent(inout) the actual argument must be definable whether or not the subroutine does this. Without no intent specified, it depends on what happens on that particular invocation of the subroutine -- if the subroutine doesn't define the variable, it is OK; if it does, than there is a problem -- cases such as writing to an actual argument that is a constant will obviously cause problems.

This doesn't mean that the compiler will diagnose all of these cases -- what the standard requires a compiler to diagnose is a different issue. It would be close to impossible to detect all errors of the intent-not-specified case requirement at compile time, since violations depend on the run-time flow of the code. It is much easier for the compiler to diagnose the intent(inout) case and warn you of problems with the code.

like image 140
M. S. B. Avatar answered Sep 25 '22 13:09

M. S. B.