19-03-2014, 03:51 PM
Programming in Fortran 95
The Basics
In this section we will look at the basics of what a program is and how to make the
program run or execute.
The non-trivial example programs can be found in the directory:
$PHYTEACH/part_2/examples
with the name of the file the same as that of the program discussed in this guide.
Some sections are more advanced and are indicated clearly indicated by a thick black
line to the right of the text. These can be skipped certainly on a first reading and
indeed you will be able to tackle the problems without using the material they discuss.
A very simple program
A program is a set of instructions to the computer to perform a series of operations.
Those operations will often be mathematical calculations, decisions based on
equalities and inequalities, or special instructions to say write output to the screen.
The program consists of “source code” which is “stored” in a text file. This code
contains the instructions in a highly structured form. Each computer language has a
different set of rules (or syntax) for specifying these operations. Here we will only
consider the Fortran 90/95 (F95 for short) programming language and syntax.
Running the program
Before we can run the program we must get the computer to convert this symbolic
language (F95) into instructions it can understand directly. This process is called
“compilation”. At the same time the computer will check our program source for
errors in the syntax, but not for errors in our logic! In general programs will be
assembled from source in many files; bringing all of these instructions together is
called “linking”. We perform both of these tasks using the Unix command f95.
• Type the following, the -o is an option saying where to place the output which in
this case is a program which is ready to run, we call this an executable. (The
default executable name is a.out).
Variables and expressions
The most important concept in a program is the concept of a variable. Variables in a
program are much like variables in an algebraic expression, we can use them to hold
values and write mathematical expressions using them. As we will see later F95
allows us to have variables of different types, but for now we will consider only
variables of type real. Variables should be declared before they are used at the start
of the program. Let us use another example to illustrate the use of variables.
Logical controls
So far all the programming statements we have met will simply enable us to produce
efficient calculators. That is useful, but there is a lot more to programming. In this
and Section 1.8 we introduce two crucial ideas. The first is the idea of taking an
action conditional upon a certain criteria being met. An example will help to
introduce this idea. For many years it was the case in Part IA of the Tripos that your
maths mark was only included if it improved your overall result. Let us write a
program to perform that simple sum. We read in four marks and output a final
average.