Fortran

Guide To Learn

1. Writing reusable code with functions and subroutines

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

When do you use a subroutine over a function?

Whenever I write a new procedure, I use the rules of thumb shown in figure 3.8 to decide whether to make it a function or a subroutine. Figure 3.8 Deciding when to use a subroutine over a function This is a simple decision-making process that you can follow. If you know your procedure will cause […]

Defining and calling a subroutine

Let’s see the difference between a subroutine and a function in an example. The following listing defines a subroutine add that’s equivalent to our function sum from the previous subsection. Listing 3.13 A subroutine that calculates the sum of two integers ❶ Inputs arguments like before ❷ Outputs argument that’s returned to the caller Here, a and b are input arguments–notice the intent(in) attribute just like in the sum function–and res is the […]

Modifying program state with subroutines

I mentioned earlier that Fortran has two kinds of procedures: functions and subroutines. Many rules that we covered for functions apply to subroutines as well. They’re both designed to be reused many times, and both may have input and output arguments. Unlike functions, subroutines can’t be used in expressions and can only be invoked in a […]

Expressing finite difference as a function in the tsunami simulator

You now understand how to define a function and how to call it from the main program. Finally, we get to the fun part–applying our new knowledge about functions to refactor our tsunami simulator. Let’s look back at the main time loop in our program, as reprised in the following listing. Listing 3.9 The time […]

Your first function

Let’s jump straight into it and write our first custom function. We’ll take our cold front program from listing 3.2 and rewrite it to delegate the temperature calculation to an external function. This will allow us to easily compute the solution for a series of different input values. Specifically, we’ll iterate over several values of […]

Don’t repeat yourself, use procedures

Like I mentioned earlier, procedures allow you to define snippets of code as their own self-contained units of functionality. You can then use them and reuse them as much as you need, by passing different values of input parameters and getting the results back. They’re similar to the main program, in that they can include […]

An overview of Fortran program units

When I introduced program as the main Fortran program unit in the previous chapter, I also mentioned a few others: functions, subroutines, and modules. Functions and subroutines, which are the focus of this chapter, are both kinds of procedures. A procedure is a miniprogram that can be called any number of times from the main program, or […]

Scroll to top