To modify an input argument in-place, define it with the intent(in out) attribute, as shown in the following listing.
Listing 3.21 A subroutine that modifies an input argument in-place
subroutine add(a, b)
integer, intent(in out) :: a ❶
integer, intent(in) :: b
a = a + b ❷
end subroutine add
❶ Uses intent(in out) to indicate that a is both an input and an output
❷ We can modify a directly; it will be returned to the calling program or procedure.
You can think of the intent attribute as a filter. intent(in) says that the arguments with this attribute can only come in, but not leave. Likewise, intent(out) allows the argument to be emitted to the calling program or procedure, but it can’t be used as an input to this procedure. intent(in out) removes these restrictions and allows an argument to be passed as an input, modified within the procedure, and then emitted back to the calling program or procedure.