Below you will find pages that utilize the taxonomy term “Fortran”
Fortran: Derived Types
We’ve seen types and variables and functions and subroutines. Now we can get to the next level and combine both variables and procedures in a so-called derived type.
Declaring a derived type and its data components
Object-oriented programming languages have classes. It took me some time to understand that classes can be seen as “extensions of the type system”. There are often primitive types like strings, integers, etc. and we can group them as properties of a class. Thereby, the class becomes some kind of advanced, composite type.
Fortran: Functions and Subroutines
So far we’ve put code inside the main program
block of our application. Soon the need arises to move blocks of code to a better place: a procedure with a proper name living inside a module
which groups related procedures by topic.
Fortran has two types of procedures:
- Functions, with arguments and a return value
- Subroutines, which are functions without a return value
Subroutines
Let’s define a new subroutine in a module
:
Fortran: Types and Variables
We’ve looked at the program
and module
blocks that can hold our code. Now we’ll find out where to put our data.
Declaration comes first
In Fortran, every time we want to store some value in a variable, we have to explicitly declare the type and the name of the variable first. This has to happen before any other executable statement, but after imports and other declarations like the implicit none
statement:
Fortran: Programs and modules
We have a basic Fortran application and a working environment to edit, compile and run it, thanks to FPM, IFX and Visual Studio Code. Let’s take a look at the structure of our very basic application.
The program
keyword
The minimum amount of code for a Fortran executable is this:
program the_program
! Do something here
end program the_program
Comment lines start with
!
. To write a multi-line comment, just repeat!
at the beginning of every line.
Running a simple Fortran program
In the previous post I included the obligatory “hello world” snippet. It would be nice if you could actually run code like that on your computer! It’s not so hard; we have very modern tooling available, and everything works equally well on Windows and Linux. The tools are freely available too.
First, install the following programs:
- Intel oneAPI Fortan Esstentials. This includes the IFX compiler.
- Fortran Package Manager (FPM). This tool is a “zero-configuration” build system which can also install and build dependencies for you.
- Visual Studio Code. We’ll use this as our editor. As far as I know there is no fully-integrated coding solution for Fortran programs. Some may use Visual Studio, some Visual Studio Code, and Jetbrains Clion may be a good option too. But none of these have solutions for all your IDE needs. After some experimenting, I think VS Code is the best option.
In a terminal, navigate to a place where you would like to create your Fortran project, and run:
Hello, Fortran world!
program hello_world
implicit none(type, external)
print *, 'Hello, world!'
end program hello_world
Since January 2024 I’m working with a smart group of programmers at Deltares. They create and maintain software in the complex “business” domains of hydrodynamics, morphology, water quality and ecology. Their software is used to understand and predict all kinds of phenomena related to water, by which they “Enable Delta life”. And that’s not just for the Netherlands (many of us here live below sea level or close to rivers); the software is used around the world, by governments and businesses.