Skip to main content
main-content
Top

About this book

In response to feedback from course delegates this third edition has been revised throughout. It expands on the second edition with new and updated examples in the chapters on arithmetic, i/o, character data, modules, data structuring and generic programming with minor updates to the rest of the chapters.

Key Features

· lots of clear, simple examples highlighting the core language features of modern Fortran including data typing, array processing, control structures, functions, subroutines, modules, user defined types, pointers, operator overloading, generic programming, object oriented programming and parallel programming

· pinpoints common problems that occur when programming

· illustrates the use of several compilers

· with better standards conformance in compilers there are new examples illustrating the following major features:

- C Interop

- IEEE arithmetic

- parameterised derived types

Introduction to Programming with Fortran will appeal to the complete beginner, existing

Fortran programmers wishing to update their code 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 Modeling Language—UML, a general purpose modeling 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 type s:
  • 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:
  • The Fortran rules for the evaluation of arithmetic expressions to ensure that they are evaluated as you intend;
  • The idea of truncation and rounding;
  • The use of the parameter attribute to define or set up constants;
  • The use of Fortran’s kind types to determine and control the precision by which arithmetic in Fortran is carried out;
  • The concept of numeric models and positional number systems for integer and real arithmetic and their implementation on binary devices;
  • Testing the numerical representation of different integer kind types on a system—8, 16, 32 and 64 bit integers;
  • Testing the numerical representation of different real kind types on a system—32, 64, 80 and 128 bit reals;
  • round off;
  • relative error;
  • absolute error.
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 loop s, 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 descriptor s. There is also coverage of how to write the results to a file, rather than to the screen. There are examples which will illustrate the use of
  • The i edit descriptor for integer data
  • The f edit descriptor for real data
  • The e edit descriptor for real data
  • The g edit descriptor for real data
  • The x edit descriptor for spaces
  • The a edit descriptor for character data
  • Repetition of edit descriptor s
  • New lines
  • Output using array section s
  • Output using whole array s
  • The open, write, and close statements.
We will also provide a brief summary of the rest of the control and data edit descriptor s, as people may see them in existing code.
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 role of functions in a programming language and in the problem-solving process is considerable and includes.
Ian Chivers, Jane Sleightholme

Chapter 13. Control Structures

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:
    • Logical expressions and logical operators .
    • One or more blocks of statements.
  • The if then endif construct .
  • The if then else if endif construct .
  • To introduce the case statement with examples.
  • To introduce the do loop , in three forms with examples, in particular:
    • The iterative do loop .
    • The do while form.
    • The do ...if then exit end do or repeat until form.
    • The cycle statement .
    • The exit statement .
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 expression s 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 list
  • 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 Examples

The aims of this chapter are to look at several mathematical examples in Fortran.
  • Using linked list s for sparse matrix problems .
  • The solution of a set 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.
  • Diagonal extraction of a matrix.
  • The solution of a system of linear simultaneous equations using Gaussian Elimination
  • An elemental e**x function
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. Object Oriented Programming

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

Chapter 29. Introduction to Parallel Programming

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

Chapter 30. 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 31. OpenMP

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

Chapter 32. Coarray Fortran

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

Chapter 33. C Interop

This chapter looks briefly at C interoperability.
Ian Chivers, Jane Sleightholme

Chapter 34. 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.
Ian Chivers, Jane Sleightholme

Chapter 35. Miscellaneous Examples

We look at a number of additional examples including
  • the earlier date derived type extended with US and ISO date formats
  • a binary search example
  • three sorting examples, including a recursive algorithm, a non recursive algorithm and a parallelised subroutine from the Nag library
    • timing details for our generic serial Quicksort algorithm for five of the numeric kind types
    • timing details of the Netlib serial non recursive Quicksort for 32 bit integers , 32 bit reals and 64 bit reals
    • a comparison of the timing of the above two sorting algorithms
    • the Nag SMP sorting routine m01caf for 64 bit reals
    • timing details of the parallel Nag sorting subroutine
  • graphics libraries
  • the dislin graphics library
  • examples calling the dislin library.
Ian Chivers, Jane Sleightholme

Chapter 36. Converting from Fortran 77

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