Code
fortran
! Function with intent
function square(x) result(y)
real, intent(in) :: x
real :: y
y = x * x
end function
! Subroutine with intent(out) — modifies argument
subroutine swap(a, b)
real, intent(inout) :: a, b
real :: temp
temp = a; a = b; b = temp
end subroutine
! Pure function (no side effects)
pure function norm(vec) result(n)
real, intent(in) :: vec(:)
real :: n
n = sqrt(sum(vec**2))
end function
! Elemental — works on scalars and arrays
elemental function deg2rad(deg) result(rad)
real, intent(in) :: deg
real :: rad
rad = deg * 3.14159265 / 180.0
end function
! Usage
print *, square(5.0) ! 25.0
call swap(x, y) ! modifies x and y
print *, norm([3.0, 4.0]) ! 5.0
print *, deg2rad([0.0, 90.0, 180.0]) ! array version (elemental)