# Introduction

February 2020

Back to index.html.

# Introduction

Yutaka Masuda

### Purpose

The purpose of this tutorial is to provide the audience with a brief idea of programming in Fortran. This tutorial focuses only on the essential elements of language and software development. It mainly covers (a subset of) the language syntax, a work-flow to develop a program, and a way to find an error in the program. The goal is to implement simple numerical algorithms in Fortran. Also, I try to give readers a way of (procedural) programming so that they can easily move to the other programming languages.

### Audience

This tutorial was written for people, working in quantitative genetics (especially animal breeding) where heavy numerical computations are required, without formal training in computer science. The audience should have a good reason to start Fortran programming. They do not have to have any computer-science background, and any formal experience in programming is not needed. They are expected to know the basic operations on the operation system including to start and quit the computer, to create, copy, and remove files and directories (folders), to make a new file and save it to disks in applications, to download some files, and to install new programs. They are expected to have some experience in using R or other scripting languages.

### Structure

This tutorial consists of 4 main parts. If you are new to Fortran programming, please start from the first.

1. The first part introduces you to an idea of Fortran programming, explaining how it works and what is a difference with R or Python.
2. The second explains the essential elements of Fortran languages, including principle syntax and some coding practices.
3. The third extends the second to have more details required in practical coding in addition to the setup of the programming environment.
4. The last part deals with algorithms, mainly matrix algebra, in addition to some basics.

Some textbooks try to write a useful program from the beginning, and a reader should learn both Fortran syntax and algorithms (logic) at the same time. The books written in such a style can be confusing to the readers because of trying to do two (difficult) things simultaneously. In this tutorial, one chapter deals with only one topic so that a reader can see the particular goal. Although some chapters for basic syntax may be bothersome, this structure is helpful for readers to look back at the detailed syntax.

### What it does not cover

This tutorial introduces you to a set of Fortran features and some algorithms implemented with the limited syntax. I have arbitrarily chosen the set of syntax so that it is nearly minimal to write a numerical program or to learn the other procedural programming languages.

This tutorial does not cover the following features: most of object-oriented programming, co-arrays, parallel computing, the block constructs, submodules, some intrinsic functions, some statements (do while, do concurrent, forall, and where), procedure pointers, the goto statement, line numbers, old syntax often used in FORTRAN 77, and many more. These features would be useful to solve your problems. Please see the other textbooks for the details.

### Environment

This tutorial expects the readers to use Linux or Unix (including macOS).

### Disclaimer

The author (Yutaka Masuda) is never responsible to the results of the usage of this tutorial. The author does not guarantee the correctness of the descriptions in this tutorial.

### Materials

I try to make this tutorial small and self-descriptive by selecting relevant topics for non-programmers to write a numerical code. For curious readers, I show some materials useful to learn Fortran more. These books/materials cover some missing components in this tutorial.

Ondřej Čertík maintains a website, a collection of the Fortran standards, best practices, and frequently asked questions.

For animal breeders, Ignacy Misztal’s cource note is useful to review the elements of Fortran.

The detailed syntax of Fortran 2018 is available from the ISO official website. Although the documentation is a draft of the standard, you can use it as a reference book of the Fortran syntax.

### Setup for the development environment

Please see the next chapter for details.

### Conventions

Here are the notational conventions in this tutorial.

#### Fortran code

! A Fortran program is shown by this format.
! The 3 does (...) means the suspension points.
program test
implicit none
real :: x,y,z
...
end program test

#### Output

This frame shows the output of a program in the screen.

#### Shell command

It shows some commands that you type on the shell in the terminal.

#### Statament in a general form

if(condition) then
{block}
else
{other block}
end if

### FORTRAN 77 vs Fortran 90/95/2003/2008/2018

This tutorial deals only with the newer Fortran standard.

Fortran has a long history, and its syntax was defined in the industry standard. The standard has been revised several times, and sometimes the changes between revisions were drastic. The biggest change was between FORTRAN 77 (published in 1978) and Fortran 90 (published in 1992). These two languages look very different, but Fortran 90 contains most of FORTRAN 77 features. In the end, some people are still using old-style in their textbooks and on-line materials. Because the old style often confuses the beginners, this tutorial is based only on Fortran 2003 or later (including all the syntax in Fortran 90/95).

The capital FORTRAN refers to the old language (77 or earlier), and Fortran is for 90 or later. Especially, Fortran 2003 (or later) is called Modern Fortran. We have Fortran 90, 95, 2003, 2008, and 2018, and the newest standard is so-called Fortran 2018. I will use the word Fortran as the language we look at. Also, I may mention FORTRAN 77 just in comparison with Modern Fortran.

### What is the difference between C/C++ and Fortran

C and C++ are general-purpose languages, and Fortran is specialized in numerical computations. C and C++ are often used to develop system applications like operating systems, device drivers, and business software (word processors, database systems, etc.). Fortran is chosen for heaving computing on super-computers or high-performance computers.

Although C++ has many elements of C, two languages are different. C is a procedural programming language, and it is much simpler than C++. C++ is an object-oriented programming language with tons of useful features for any purpose. Probably C is “simpler” than Fortran, but the programmer may need more knowledge of how computers work. C++ is way more complicated than Fortran.

### What is the difference between R/Python and Fortran

R and Python (Ruby, Perl, and many more languages) are scripting languages, and Fortran (C, C++, and many more) is a compiled language. A program written in a scripting language runs in a special environment; there are many steps in which the computer executes the code. A program written in a compiled language is converted to the machine code before the execution, and the computer directly reads and execute the code.

For programmers, the scripting language is generally more natural to write, run, and test because it usually supports high-level and comprehensive functions to perform a complicated task with a simple syntax. Also, frequently-used functions are built-in for many scripting languages. Instead, it tends to be slow in running because the interpretation of code is costly. The compiled language is usually faster in execution because the entire program has been translated to machine-readable code. It needs more effort to finish programming and to make sure the code correctly works as the size of the program increases.

The development process is like house building. The programming in R/Python combines pre-built materials with advanced tools; you simply combine them, but you would not change the details (and the change is often costly). In Fortran, you have to use only primitive tools and raw materials; you have to do from scratch, but you can control the details. Running a program in R or Python is like having an interpreter between the person in charge and the worker, translating the boss’s instructions one by one, and using it for the worker. Fortran prepares instructions perfectly understandable for workers.

### Way of programming: R vs Fortran

In this subsection, we are looking at a more detailed difference between Fortran and R, a popular language in quantitative genetics.

Think about computing the mean and the standard deviation of univariate data stored in a file. In R (Base R), a typical workflow looks like this.

data <- read.table("data.txt",header=FALSE)
apply(data,2,mean)
apply(data,2,sd)

It describes the workflow of data analysis. The actual operation behind each function is invisible to users so that the programmers can focus on logic. The first step of R programming is to find packages or functions, which are already prepared in the system or by other developers. The function takes care of errors without serious troubles. The users write a course of data manipulation and analyses by combining such functions without much effort to develop computational details. The productivity of coding is high if appropriate functions are used. An issue is that the running performance depends on the functions; sometimes, the code is very slow or impossible to run. Also, if a feature is not available or unstable, the user must modify the existing code or write a new program that may have the performance issue.

Fortran does not provide high-level features. Fortran has statements (like commands) and fundamental but efficient functions to perform simple tasks. The programmer should describe all details including how much memory is needed to store a vector, what kind of data is requied in the program, how to compute numbers the user really needs, and how to do with errors. In other words, the programmer should code algorithms by a combination of basic commands.

Here is a Fortran program equivalent to the above R code. Note that I wrote this code in modern Fortran with new features. If you write down a robust program (maximizing the performance), it may be twice longer than this one.

program mean_and_sd
implicit none
integer :: unit,info,n
real :: val,mean
real,allocatable :: data(:)

n = 0
allocate(data(0))
open(newunit=unit,file="data.txt",status="old")
do
if(info/=0) exit
n = n + 1
data = [data, x]
end do
close(unit)

if(n>=1) then
mean = sum(data)/n
print *,"mean = ",mean
if(n>=2) then
print *,"sd   = ",sum((data-mean)**2)/(n-1)
end if
end if
end program mean_and_sd

The program is lengthy, and the programmer should write all the details. Instead, this code is performant in the exchange of productivity in coding. Although developing high-level functions is time-consuming, once it is done, you can reuse the efficient code in any program. It is a reason why Fortran is still alive in high-performance computing, and old Fortran programs are often alive in some scientific fields.

Back to index.html.