Fortran

Guide To Learn

Core elements of Fortran

Getting compiler version and options

Once you have a compiled program executable, it’s not obvious how it was compiled. Specifically, what compiler was used, and were any compiler options used–for example, for debugging or optimization? Fortran’s iso_fortran_env module provides two functions that allow you to get this information at runtime: compiler_version and compiler_options. You get the idea what each of these functions does. Let’s write a program that […]

Accessing a module

Before we venture into writing our own custom modules, let’s first get familiar by accessing a built-in module that comes with Fortran out of the box. Fortran has a few built-in modules. The most commonly used one is iso_fortran_env, which, among other things, provides constants and procedures that allow you to write more portable programs. Another commonly […]

Exercise 2: Writing an elemental function that operates on both scalars and arrays

The solution is to add the pure elemental attributes to our previous version of the cold front program, as shown in the following listing. Listing 3.22 Cold front temperature function that works with scalars and arrays ❶ The elemental attribute makes the function compatible with both scalars and arrays. You can now invoke this function with one or more […]

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

Scroll to top