A Basic Program#

Introduction#

Fortran files have the extension .f90 1 and begin with the program keyword. They end with the corresponding end program keywords. It is good practice to label your program, for instance below is a Fortran program called hello_world inside the file hello_world.f90.

Listing 1 hello_world.f90#
program hello_world
    print *, 'Hello World!'
end program hello_world

Fortran is case insensitive, this book will use lowercase as part of its style. Notice the indented print keyword, the Fortran standard doesn’t include tabs so the indentation is four spaces for each level.

Compiling#

Fortran is a compiled language. To compile our hello_world.f90 program we can use the following commands:

gfortran -o hello hello_world.f90
ifort -o hello hello_world.f90

And to run our program from the terminal:

> ./hello
Hello World!
> hello.exe
Hello World!

Comments#

Comments in Fortran are declared with an !:

! This is a comment

As you work through the exercises in this book make sure you comment your code! Generating full documentation for your code will be explored in the Doxygen section.

Summary#

Exercises#

Cards like the one below describe your next exercise and are normally followed by a solution card. These exercises are intended to be coded on your local machine and compiled. Have a go at your first exercise, building the hello world program and running it.

Exercise 1

For this exercise:

  • Ensure that you have a Fortran compiler present on your system

  • Create the file hello_world.f90

  • Copy the hello_world.f90 program from above

  • Compile and run the program


1

Fortran files can have other extensions, .f90 is used to denote free-form Fortran which removed fixed form restrictions such as the line width being constrained to 72 columns, for more see Free vs. Fixed Format. Fixed form Fortran is denoted by the .f and .for extensions. If a Fortran file requires preprocessing use a capital F in the extension, .F90. For more see the preprocessing section of this book.