029. Subroutines can have intent(in out) arguments, but functions should not#

topic: Procedures

A procedure argument can be intent(in out), which means that it is an input that can be overwritten. Functions should not have such arguments, but subroutines often do. intent(out) means the argument is set to uninitialized upon entering the procedure.

intent-in-out.f90 | | 0 | Godbolt Compiler Explorer logo | Fortran logo#
program intent_in_out
   implicit none
   real :: x(3)

   x = [1., 4., 5.]
   call normalize(x)
   print *, x  ! 0.1 0.4 0.5

contains

   subroutine normalize(x)
      ! Scale `x` so that `sum(x)` = 1.
      real, intent(in out) :: x(:)
      real                 :: xsum
      xsum = sum(x)
      if (xsum /= 0) x = x/xsum
   end subroutine normalize

end program intent_in_out
Output1#
  0.100000001      0.400000006      0.500000000    


1

Compiled using GNU Fortran (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0 with no flags