Fortran The Intent of Dummy Arguments


Example

The intent attribute of a dummy argument in a subroutine or function declares its intended use. The syntax is either one of

intent(IN)
intent(OUT)
intent(INOUT)

For example, consider this function:

real function f(x)
  real, intent(IN) :: x

  f = x*x
end function

The intent(IN) specifies that the (non-pointer) dummy argument x may never be defined or become undefined throughout the function or its initialization. If a pointer dummy argument has the attribute intent(IN), this applies to its association.

intent(OUT) for a non-pointer dummy argument means that dummy argument becomes undefined on invocation of the subprogram (except for any components of a derived type with default initialization) and is to be set during execution. The actual argument passed as dummy argument must be definable: passing a named or literal constant, or an expression, is not allowed.

Similarly to before, if a pointer dummy argument is intent(OUT) the association status of the pointer becomes undefined. The actual argument here must be a pointer variable.

intent(INOUT) specifies that the actual argument is definable and is suitable for both passing in and returning data from the procedure.

Finally, a dummy argument may be without the intent attribute. Such a dummy argument has its use limited by the actual argument passed.

For example, consider

integer :: i = 0
call sub(i, .TRUE.)
call sub(1, .FALSE.)

end

subroutine sub(i, update)
  integer i
  logical, intent(in) :: update
  if (update) i = i+1
end subroutine

The argument i can have no intent attribute which allows both of the subroutine calls of the main program.