Modules and Constants
Pure Functions
No Side Effects
A pure function promises no side effects and no I/O. The compiler enforces it.
Program
Play the program to square a number with a pure function.
pure_function.f90
module math_mod
implicit none
contains
pure function square(x) result(y)
integer, intent(in) :: x
integer :: y
y = x * x
end function square
end module math_mod
program pure_function_demo
use math_mod
implicit none
integer :: result_
result_ = square(9)
print '(I0)', result_
end program pure_function_demo
pure
`pure function` cannot perform I/O or modify globals.
compile check
The compiler rejects side effects inside `pure` procedures.
functional style
Pure functions are safe to call from elemental and parallel contexts.