Basic Fortran Program

The following is a simple Hello World program written in Fortran. This example illustrates the structure of a basic Fortran program and how it should be compiled and run. The file is saved as a text file named hello.f90.

program helloWorld
! This is a comment − it doesn't do anything
! Declare an integer variable called i
integer :: i
real :: a ! Declare a real variable a
i = 5 ! Assign values to i and a
a = 3.142
! Print a message to the screen
write (∗,∗) 'Hello World'
write (∗,∗) 'This is an integer: ',i
write (∗,∗) 'This is a real: ',a
end program helloWorld

On fionn, this program can be compiled into an executable by firstly loading the Fortran compiler:

module load dev intel/latest

and then compiling the program into an executable named hello:

ifort hello.f90 −o hello

To run the program, type:

./hello

The program will produce output similar to the following:

Hello World
This is an integer: 5
This is a real: 3.142000

Fortran files are identified by a file extension, which tells the compiler what type of file it is. Older Fortran files were fixed form, meaning that some columns of the file were reserved for certain characters or statements. Fixed form files usually have the one of the following extensions: .f, .f77 or .for. Modern versions of Fortran permit the use of free form files, which place no restrictions on column usage. All modern source files should be written in free form, and saved with the extension .f90 or .f95.

Supported By

File Browser Reference
Department FHERIS
University of Galway
HEA Logo