Fortran

Guide To Learn

Exercise 1: Modifying state with a subroutine

To modify an input argument in-place, define it with the intent(in out) attribute, as shown in the following listing. Listing 3.21 A subroutine that modifies an input argument in-place ❶ Uses intent(in out) to indicate that a is both an input and an output ❷ We can modify a directly; it will be returned to the calling program or procedure. You […]

Tsunami simulator: Putting it all together

Finally, we get to put together the new function (from subsection 3.2.2) and subroutine (from subsection 3.3.3) in the main program of the tsunami simulator. In a nutshell, this program has the same functionality and behavior as the previous version from chapter 2. The key difference is that now we’ve abstracted away the code to […]

Procedures with optional arguments

Both functions and subroutines can accept optional arguments. These are arguments that may be omitted by the caller, even if they’re specified in the procedure definition. To see optional arguments in action, let’s take our subroutine add from listing 3.13 and add an optional debug input parameter, as shown in listing 3.18. If this parameter is passed by the […]

Writing procedures that operate on both scalars and arrays

When a procedure is defined to operate on scalar arguments, it’s relatively straightforward to make it work with array arguments as well. For example, recall our pure function sum from the previous subsection: Invoking this function as, say, sum(3, 5) will evaluate to 8. Is there a way to pass array arguments to this function such that it returns an array […]

Why are pure functions important?

Including a pure attribute in your function and subroutine statements forces you to write side effect-free code. This has two principal benefits: Tip Write pure procedures whenever possible. As I’ll show you later in the book, using the pure attribute can get you a long way toward functional programming with Fortran.

Some restrictions on pure procedures

A pure procedure, while advantageous from both program design and compiler optimization perspectives, does come with a number of restrictions: There are several more restrictions on pure procedures that are more situational and that you’re less likely to encounter. We’ll revisit this topic later in the book as we encounter these edge cases.

What is a pure procedure?

A Fortran procedure is pure when it doesn’t cause any observable side effects, such as I/O or modifying the value of a variable declared outside of the procedure. To define a procedure as pure, simply add the pure attribute to its function or subroutine statement, as shown in the following listing. Listing 3.17 Defining a pure, side effect-free function ❶ The pure attribute asserts that the function […]

Writing pure procedures to avoid side effects

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 […]

Initializing water height in the tsunami simulator

Recall the initialization part of our tsunami simulator from listing 2.13, which is repeated in the following listing for your convenience, where we set the initial conditions. Listing 3.15 Initializing the tsunami simulator ❶ The index at which the water perturbation will be centered ❷ The parameter that governs the width of the perturbation ❸ Sets the array […]

Scroll to top