A Trivial Program

Last updated on 2025-05-01 | Edit this page

Overview

Questions

  • How do you write programs in Fortran?
  • How do you compile the Fortran code to make an executable?
  • How do we run that executable and see any output?

Objectives

Be able to

  • write
  • compile
  • run

a basic Fortran program.

During the setup you compiled a simple hello world program. Let’s look at that program in more detail:

BASH

cd ~/Desktop/intro-to-modern-fortran/01-trivial-program
cat hello_world.f90

FORTRAN

program hello_world

    implicit none

    print *, 'Hello world!'

end program hello_world

The first statement program hello_world starts the program. It also defines the program name, hello_world. It is matched by the end statement (end program hello_world). The end statement is always the last statement in a program.

program on its own marks the start of a program and end on its own will end the program. Words that follow program or end are labels which improve readability. Some legacy codes may not have labels.

Implicit None


Some variables in Fortran have a default type. Variables with names beginning with letters i-n are implicitly of type integer Anything else is of type real.

This is very bad practice and modern Fortran should not be used in this way.

To prevent implicit typing we add the:

FORTRAN

implicit none

statement to all programs (and modules, functions, and subroutines that you will encounter in later episodes).

Now all variable names must be declared explicitly before they are referenced.

The only executable line in this program is the print statement. We will cover IO in a later episode. For now, know that print *, will print what follows to standard output when the program runs. In this case, it will print the string Hello world! to our terminal.

Comments


Comments start with an exclamation mark !. Comments can appear on their own line, or after any other Fortran statement.

You may see code that has comments written like this:

FORTRAN

c      this is a comment

with a c in the first column. In new code use the modern ! comments.

Add comments to hello_world.f90

Add two comments to your file:

  1. The first should explain what the program does
  2. The second should explain the purpose of implicit none

Think about where the best place for these comments are.

FORTRAN

program hello_world
    !! A simple hello world program

    implicit none  ! prevent implicit typing (to integers) of variables
                   ! whose name starts with the letters i-n

    print *, 'Hello world!'

end program hello_world
  1. We have placed a comment describing the program under the program statement. You might also see program descriptions before the program statement. Note the double ! at the start of the comment. This allows the automatic documentation generator FORD to extract documentation from the comment.
  2. The second comment could have been placed before or after the implicit none statement. We have shown an inline comment. Note the two spaces between the Fortran and the start of the comment, and the comment spans multiple lines with each ! aligned vertically.

You may have noticed comments at the top of the Fortran files you downloaded during the setup. Those comments provide licensing and authorship information.

Compiling


Fortran is a compiled language (like C++). A compiler turns human-readable source code into machine code. This machine code can then be executed by the computer. Languages like Python are interpreted. This means Python source code is executed directly without being compiled into machine code first. The program is parsed, interpreted, and executed each time it is run. Compiled programs are usually more efficient than interpreted programs. This is a major reason that compiled languages like Fortran remain popular. The draw back is that there is an extra step in building Fortran programs.

There are several steps1 that occur during compilation. Your compiler takes care of each step for you. To test your compiler in the setup episode you ran:

This created a file named a.out. This is the default executable name if no name is specified. To tell the compiler the name of the executable use the -o flag:

Now run ls to see the new executable:

BASH

ls

OUTPUT

hello_world  hello_world.f90

We now have an executable called hello_world. Sometimes code can compile but fail when executed2. Let’s run this executable to check it works:

BASH

./hello_world

OUTPUT

 Hello world!

Compiler Documentation and Flags

Take a moment to find and bookmark the documentation for your compiler. Find the correct flags to:

  • Turn on debugging
  • Specify the default optimisation level

Filenames


Fortran files normally end in .f90, although they can have different file extensions. The .f90 extension is the most widely recognised across compilers. You may see Fortran files with an upper-case extension .F90. This tells the compiler to pre-process the file before compiling.

In the next episode we will introduce variable declaration, and you will write your first Fortran program from scratch.

Key Points

  • Fortran programs start with the program <label> statement.
  • Fortran programs end with the end program <label> statement.
  • Always use implicit none to prevent implicit typing for variables.
  • Fortran comments start with !.
  • The -o flag specifies the name of the compiled executable: <compiler_command> -o <executable_name> <source_file.f90>.

  1. Compilation steps for a C program: https://www.geeksforgeeks.org/compiling-a-c-program-behind-the-scenes/, the steps are the same for Fortran!↩︎

  2. If a program fails to compile that’s a compiler error. If a program compiles but fails to run that’s a runtime error.↩︎