Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Valid programs showing difference between intent(out) and intent(inout) in Fortran

Tags:

fortran

This is a follow up to a post that I found on SO: Difference between intent(out) and intent(inout)

The linked question asked about the difference between intent(out) and intent(inout) in Fortran by asking an invalid program.

Could anyone come up with a simple valid program(s) that give different results by changing intent(inout) to intent(out) or vice versa?


1 Answers

Here you go...

program intent_test
implicit none
integer, allocatable :: a(:)

a = [1,2,3,4,5]

call intent_inout (a)

call intent_out (a)

contains

subroutine intent_inout (a)
integer, allocatable, intent(inout) :: a(:)

if (allocated(a)) then
  print *, a
else
  print *, "Unallocated"
end if

end subroutine intent_inout

subroutine intent_out (a)
integer, allocatable, intent(out) :: a(:)

if (allocated(a)) then
  print *, a
else
  print *, "Unallocated"
end if

end subroutine intent_out

end program intent_test

1 2 3 4 5 Unallocated

like image 156
Steve Lionel Avatar answered Dec 14 '25 08:12

Steve Lionel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!