Stefano Borini
Stefano Borini

Reputation: 143795

difference between intent(inout) and pointer dummy arguments

What is the practical difference in having

subroutine fillName(person)
   type(PersonType), intent(inout) :: person

   person%name = "Name"
end subroutine

or the following

subroutine fillName(person)
   type(PersonType), pointer :: person

   person%name = "Name"
end subroutine

Upvotes: 3

Views: 1429

Answers (2)

Simon
Simon

Reputation: 32893

If I assume the keyword is practical, then the practical difference in the example you give would be readability, since they both work but intent(inout) is more explicit.

The technical difference is that the pointer may be null or undetermined, whereas with intent(inout) the variable have to be allocated. A pointer can also be associated or nullified in the subroutine but a dummy argument with intent(inout) cannot.

If you don't specify neither pointer or intent(inout) and you pass a pointer in argument then it have to be associated.

Upvotes: 1

user7116
user7116

Reputation: 64078

pointer has specific argument requirements that the bare description does not have. Basically the dummy argument person must be associated with a pointer target. It could be through an allocation or simple pointer assignment (=>). An important thing to note is that any changes to the pointer association of the dummy argument person during the execution of the subroutine will be reflected in the actual argument passed. The bare description will pass the actual argument by reference, but not pointer association.

Upvotes: 2

Related Questions