Fortran lets you define a function or a subroutine in a way that prevents side effects. What exactly do I mean here by side effects? For a concrete example, consider the traffic grid in a major city. Roadwork on a busy road during rush hour will soon slow down the incoming traffic, causing a traffic jam miles away. This is a side effect of the roadwork; on its own, it’s a local effect, but because it impacts the incoming traffic, it causes a ripple effect in a remote part of the system. As a result, we get the repair and the hour-long traffic jam. In contrast, if the roadwork were to be scheduled in the middle of the night when there’s no or little traffic, its effects would be isolated from the rest of the system. In that case, we’d only get the repair as intended, without any adverse side effects.
A pure procedure allows you to write code that won’t affect the state of the program outside of the procedure, aside from the result that it returns. If the code somehow violates this restriction, the compiler will report an error. Pure procedures are among my favorite features of Fortran–C doesn’t have them, and neither does Python or JavaScript. Pure procedures are a pillar of functional programming, and if you make liberal use of them, you will develop code that’s easier for the compiler to optimize, and easier for you to understand and debug. Let’s see exactly what pure procedures are and how to use them.