views:

291

answers:

2

Hey.

After searching for a while in books, here on stackoverflow and on the general web, I have found that it is difficult to find a straightforward explanation to the real differences between the fortran argument intents. The way I have understood it, is this:

  • `intent(in)` -- The actual argument is copied to the dummy argument at entry.
  • `intent(out)` -- The dummy argument points to the actual argument (they both point to the same place in memory).
  • `intent(inout)` -- the dummy argument is created locally, and then copied to the actual argument when the procedure is finished.

If my understanding is correct, then I also want to know why one ever wants to use intent(out), since the intent(inout) requires less work (no copying of data).

+2  A: 
  • intent(in) - yes, pass by value, so changes of this are not reflected in outside code.
  • intent(out) - pass somehow by reference, in fact a return argument
  • intent(inout) - pass by reference, normal in/out parameter.

So your obersavations are correct.

Use intent(out) if is is plain out, to document your design. Do not care for the very little performance gain if any.

Peter Kofler
Are you sure the performance gain is insignificant? What if a function is called often and takes very large arrays as arguments?
Karl Yngve Lervåg
Sure, if you ONLY call the method in a microbenchmark, it will take a lot of time. And yes, if you pass VERY large arrays, it might be to consider. But most of the time remember, "Early optimization is the root of all evil". You can still change it later when you proove the bottleneck to be there and it's on the top 3 of things taking up your time. I would always first use the design principles.
Peter Kofler
Ok. Thank you for elaborating :)
Karl Yngve Lervåg
+1  A: 

intent(in) is not pass by value. You can still overwrite the original value. Intents are just hints for the compiler, and you can throw that information away and violate it. Intents exists almost entirely to make sure that you only do what you planned to do in a subroutine. A compiler might choose to trust you and optimize something.

program xxxx
integer i
i = 9
call sub(i)
print*,i ! will print 7 on all compilers I checked
end
subroutine sub(i)
integer,intent(in) :: i
call sub2(i)
end
subroutine sub2(i)
implicit none
integer i
i = 7 ! This works since the "intent" information was lost.
end

program xxxx
integer i
i = 9
call sub(i)
end
subroutine sub(i)
integer,intent(out) :: i
call sub2(i)
end
subroutine sub2(i)
implicit none
integer i
print*,i ! will print 9 on all compilers I checked, even though intent was "out" above.
end

The Glazer Guy