Skip to main content
main-content
Top

About this book

This fourth Edition presents new examples on submodules, derived type i/o, object oriented programming, abstract interfaces and procedure pointers, C interop, sorting and searching, statistics and converting to more modern versions of Fortran.

Key Features

Highlights the core language features of modern Fortran including data typing, array processing, control structures, functions, subroutines, modules and submodules, user defined types, pointers, operator overloading, generic programming, parallel programming, abstract interfaces, procedure pointers

Pinpoints common problems that occur when programming

Illustrates the use of several compilers

Introduction to Programming with Fortran has been written for the complete beginner with little or no programming background as well as existing Fortran programmers and those with programming experience in other languages

Table of Contents

Chapter 1. Overview

The aims of the chapter are to provide a background to the organisation of the book.
Ian Chivers, Jane Sleightholme

Chapter 2. Introduction to Problem Solving

The aims of this chapter are:
  • To examine some of the ideas and concepts involved in problem solving.
  • To introduce the concept of an algorithm.
  • To introduce two ways of approaching algorithmic problem solving.
  • To introduce the ideas involved with systems analysis and design, i.e., to show the need for pencil and paper study before using a computer system.
  • To introduce the Unified modelling Language - UML, a general purpose modelling language used in the field of software engineering.
Ian Chivers, Jane Sleightholme

Chapter 3. Introduction to Programming Languages

The primary aim of this chapter is to provide a short history of program language development and give some idea as to the concepts that have had an impact on Fortran. It concentrates on some but not all of the major milestones of the last 40 years, in roughly chronological order. The secondary aim is to show the breadth of languages available. The chapter concludes with coverage of a small number of more specialised languages.
Ian Chivers, Jane Sleightholme

Chapter 4. Introduction to Programming

The aims of the chapter are:
  • To introduce the idea that there is a wide class of problems that can be solved with a computer and, further, that there is a relationship between the kind of problem to be solved and the choice of programming language that is used.
  • To give some of the reasons for the choice of Fortran.
  • To introduce the fundamental components or kinds of statements to be found in a general purpose programming language.
  • To introduce the three concepts of name, type and value.
  • To illustrate the above with sample programs based on three of the five intrinsic data types:
  • character, integer and real.
  • To introduce some of the formal syntactical rules of Fortran.
Ian Chivers, Jane Sleightholme

Chapter 5. Arithmetic

The aims of this chapter are to introduce.
Ian Chivers, Jane Sleightholme

Chapter 6. Arrays 1: Some Fundamentals

The aims of the chapter are to introduce the fundamental concepts of arrays and do loops, in particular:
  • To introduce the idea of tables of data and some of the formal terms used to describe them:
    • Array.
    • Vector.
    • List and linear list.
  • To discuss the array as a random access structure where any element can be accessed as readily as any other and to note that the data in an array are all of the same type.
  • To introduce the twin concepts of data structure and corresponding control structure.
  • To introduce the statements necessary in Fortran to support and manipulate these data structures.
Ian Chivers, Jane Sleightholme

Chapter 7. Arrays 2: Further Examples

The aims of the chapter are to extend the concepts introduced in the previous chapter and in particular:
  • To set an array size at run time - allocatable arrays.
  • To introduce the idea of an array with more than one dimension and the corresponding control structure to permit easy manipulation of higher-dimensioned arrays.
  • To introduce an extended form of the dimension attribute declaration, and the corresponding alternative form to the do statement, to manipulate the array in this new form.
  • To introduce the do loop as a mechanism for the control of repetition in general, not just for manipulating arrays.
  • To formally define the block do syntax.
Ian Chivers, Jane Sleightholme

Chapter 8. Whole Array and Additional Array Features

The aims of the chapter are:
  • To look more formally at the terminology required to precisely describe arrays.
  • To introduce ways in which we can manipulate whole arrays and parts of arrays (sections).
  • To introduce the concept of array element ordering and physical and virtual memory.
  • To introduce ways in which we can initialise arrays using array constructors.
  • To introduce the where statement and array masking.
  • To introduce the forall statement and construct.
  • Physical and virtual memory
  • Type declaration statement summary.
Ian Chivers, Jane Sleightholme

Chapter 9. Output of Results

The aims here are to introduce some of the facilities for producing neat output using edit descriptors. There is also coverage of how to write the results to a file, rather than to the screen.
Ian Chivers, Jane Sleightholme

Chapter 10. Reading in Data

The aims of this chapter are to introduce some of the ideas involved in reading data into a program. In particular, using the following:
  • Reading from files
  • Reading integer data
  • Reading real data
  • Skipping columns of data in a file
  • Skipping lines in a file
  • Reading from several files consecutively
  • Reading using internal files
  • Timing of formatted and unformatted reads
Ian Chivers, Jane Sleightholme

Chapter 11. Summary of I/O Concepts

This chapter covers more formally some of the concepts introduced in Chaps. 9 and 10. There is a coverage of
  • I/O concepts and I/O statements
  • Files, records and streams
  • Sequential, direct and stream access
  • Options or specifiers on the open statement
  • Options or specifiers on the close statement
  • Options or specifiers on the write statement
  • Options or specifiers on the read statement. .
Ian Chivers, Jane Sleightholme

Chapter 12. Functions

The aims of this chapter are:
  • To consider some of the reasons for the inclusion of functions in a programming language.
  • To introduce, with examples, some of the predefined functions available in Fortran.
  • To introduce a classification of intrinsic functions, generic, elemental , transformational.
  • To introduce the concept of a user defined function.
  • To introduce the concept of a recursive function.
  • To introduce the concept of user defined elemental and pure functions.
  • To briefly look at scope rules in Fortran for variables and functions.
  • To look at internal user defined functions.
Ian Chivers, Jane Sleightholme

Chapter 13. Control Structures and Execution Control

The aims of this chapter are to introduce:
  • Selection among various courses of action as part of the algorithm.
  • The concepts and statements in Fortran needed to support the above:
    • execution control.
    • executable constructs containing blocks.
      • the associate construct.
      • the block construct.
      • the do construct.
      • the if construct.
      • the select case construct.
      • the select rank construct.
      • the select type construct.
    • Logical expressions and logical operators.
    • One or more blocks of statements.
Ian Chivers, Jane Sleightholme

Chapter 14. Characters

The aims of this chapter are:
  • To extend the ideas about characters introduced in earlier chapters.
  • To demonstrate that this enables us to solve a whole new range of problems in a satisfactory way.
Ian Chivers, Jane Sleightholme

Chapter 15. Complex

The aims of this chapter are:
  • To introduce the last predefined numeric data type in Fortran.
  • To illustrate with examples how to use this type.
Ian Chivers, Jane Sleightholme

Chapter 16. Logical

The aims of this chapter are:
  • To examine the last predefined type available in Fortran: logical.
  • To introduce the concepts necessary to use logical expressions effectively:
    • Logical variables.
    • Logical operators.
    • The hierarchy of operations.
    • Truth tables.
Ian Chivers, Jane Sleightholme

Chapter 17. Introduction to Derived Types

The aim of this chapter is to introduce the concepts and ideas involved in using the facilities offered in modern Fortran for the construction and use of derived or user defined types;
  • defining our own types.
  • declaring variables to be of a user defined type.
  • manipulating variables of our own types.
  • nesting types within types.
Ian Chivers, Jane Sleightholme

Chapter 18. An Introduction to Pointers

The primary aim of the chapter is to introduce some of the key concepts of pointers in Fortran.
Ian Chivers, Jane Sleightholme

Chapter 19. Introduction to Subroutines

The aims of this chapter are:
  • To consider some of the reasons for the inclusion of subroutines in a programming language.
  • To introduce with a concrete example some of the concepts and ideas involved with the definition and use of subroutines.
    • Arguments or parameters.
    • The intent attribute for parameters.
    • The call statement.
    • Scope of variables.
    • Local variables and the save attribute.
    • The use of parameters to report on the status of the action carried out in the subroutine.
  • Module procedures to provide interfaces.
Ian Chivers, Jane Sleightholme

Chapter 20. Subroutines: 2

The aims of this chapter are to extend the ideas in the earlier chapter on subroutines and look in more depth at parameter passing, in particular using a variety of ways of passing arrays.
Ian Chivers, Jane Sleightholme

Chapter 21. Modules

The aims of this chapter are to look at the facilities found in Fortran provided by modules, in particular.
  • The use of a module to aid in the consistent definition of precision throughout a program and subprograms.
  • The use of modules for global data.
  • The use of modules for derived data types.
  • Modules containing procedures
  • A module for timing programs
  • Public, private and protected attributes
  • The use statement and its extensions
Ian Chivers, Jane Sleightholme

Chapter 22. Data Structuring in Fortran

The aims of this chapter are to look at several complete examples illustrating data structuring in Fortran using the following
  • Singly linked lists
  • Ragged arrays
  • A perfectly balanced tree
  • A date data type
Ian Chivers, Jane Sleightholme

Chapter 23. An Introduction to Algorithms and the Big O Notation

The aims of this chapter are to provide an introduction to algorithms and their behaviour. In Computer Science this is normally done using the so called big O notation.
Ian Chivers, Jane Sleightholme

Chapter 24. Operator Overloading

The aims of this chapter are to look at operator overloading in Fortran.
Ian Chivers, Jane Sleightholme

Chapter 25. Generic Programming

This chapter looks at some examples that implement generic programming in Fortran.
Ian Chivers, Jane Sleightholme

Chapter 26. Mathematical and Numerical Examples

The aims of this chapter are to look at several mathematical and numeric examples in Fortran.
  • Using linked lists for sparse matrix problems.
  • The solution of a system of ordinary differential equations using the Runge–Kutta–Merson method, with the use of a procedure as a parameter, and the use of work arrays.
  • The use of optional and keyword arguments
  • Diagonal extraction of a matrix.
  • The solution of a system of linear simultaneous equations using Gaussian Elimination
  • An elemental e**x function
  • Examples of the relative and absolute errors involved in subtraction with 32 and 64 bit precision
Ian Chivers, Jane Sleightholme

Chapter 27. Parameterised Derived Types (PDTs) in Fortran

The aims of this chapter are to look at some additional data structuring examples in Fortran that use parameterised derived types - PDTs.
Ian Chivers, Jane Sleightholme

Chapter 28. Introduction to Object Oriented Programming

The aims of this chapter are to look at object oriented programming in Fortran.
Ian Chivers, Jane Sleightholme

Chapter 29. Additional Object Oriented Examples

The aim of this chapter are to look at some additional object oriented programming examples in Fortran.
Ian Chivers, Jane Sleightholme

Chapter 30. Introduction to Submodules

The aims of this chapter is to provide a short introduction to submodules.
Ian Chivers, Jane Sleightholme

Chapter 31. Introduction to Parallel Programming

The aims of this chapter is to provide a short introduction to parallel programming.
Ian Chivers, Jane Sleightholme

Chapter 32. MPI - Message Passing Interface

The aims of this chapter is to provide a short introduction to MPI programming in Fortran.
Ian Chivers, Jane Sleightholme

Chapter 33. OpenMP

The aims of this chapter is to provide a short introduction to OpenMP programming in Fortran.
Ian Chivers, Jane Sleightholme

Chapter 34. Coarray Fortran

The aims of this chapter is to provide a short introduction to coarray programming in Fortran.
Ian Chivers, Jane Sleightholme

Chapter 35. C Interop

This chapter looks briefly at C interoperability. C is a widely used programming language and there is a considerable amount of software written in C or with a C calling interface. Fortran 2003 introduced a standardised mechanism for interoperating with C. There were limitations to this interoperability and ISO TS 29113 significantly extended the scope of the interoperation facilities. The TS was published in 2012. In this chapter we provide a brief coverage of some of the technical details required for interoperability and then have a look at a couple of examples.
Ian Chivers, Jane Sleightholme

Chapter 36. IEEE Arithmetic

The aims of this chapter are to look in more depth at arithmetic and in particular at the support that Fortran provides for the IEEE 754 and later standards. There is a coverage of:
  • hardware support for arithmetic.
  • integer formats.
  • floating point formats: single and double.
  • special values: denormal, infinity and not a number — nan.
  • exceptions and flags: divide by zero, inexact, invalid, overflow, underflow.
Ian Chivers, Jane Sleightholme

Chapter 37. Derived Type I/O

In this chapter we look at a facility introduced in the Fortran 2003 standard - derived type I/O. The Fortran 2018 standard calls it defined type input/output, and is now widely available in current compilers. When a derived type is encountered in an I/O list, we can arrange to call a Fortran subroutine. For a particular derived type and a particular set of kind type parameter values, there are four possible sets of characteristics for defined input/output procedures; one each for
  • formatted input
  • formatted output
  • unformatted input
  • unformatted output
Ian Chivers, Jane Sleightholme

Chapter 38. Sorting and Searching

We look at a number of sorting and searching examples.
Ian Chivers, Jane Sleightholme

Chapter 39. Handling Missing Data in Statistics Calculations

In this chapter we look at a case study of processing the Met Office historic data files and generating statistics accommodating missing data values. Several steps are involved
  • a program to download and save the data files locally.
  • a sed script to convert the missing values.
  • a modified statistics module that will process and report on missing values.
  • a module that encapsulates the Met Office station data information.
  • a program that actually does the calculations and generates the summary information.
  • a site description module that encapsulates the site information.
  • a program to generate the site information summary data.
Ian Chivers, Jane Sleightholme

Chapter 40. Converting from Fortran 77

This chapter looks at some of the options available when working with older Fortran code.
Ian Chivers, Jane Sleightholme

Chapter 41. Graphics Libraries - Simple Dislin Usage

In science and engineering graphics are essential part of the presentation of information.
Ian Chivers, Jane Sleightholme

Chapter 42. Abstract Interfaces and Procedure Pointers

We look at an example that illustrates the use of abstract interfaces and procedure pointers.
Ian Chivers, Jane Sleightholme
Additional information