• No results found

First steps in Fortran 90 programmIng•

2.2 Some basic Fortran 90 concepts

The program written in Example 2.1 is a very simple one, but it does contain many of the basic building blocks and concepts which apply to all Fortran 90 programs. We shall therefore examine it carefully line by line to establish these concepts before we move on to look at the language itself in any detail. Before doing so, however, we must emphasize that the code shown in Example 2.1 is not the whole program, since the procedure calculate3ircle is also part of the same program. What is shown is simply the main program or, more correctly, the main program unit. We shall have more to say about the other types of program unit later.

The first line of our program reads PROGRAM circle

Every main program unit must start with a PROGRAM statement which consists of the word PROGRAM followed by the name of the program. This name must follow the rules which apply to all Fortran 90 names, namely

• It must begin with a letter - either upper or lower case

• It may only contain the letters A-Z and a-z, the digits 0-9, and the underscore character

• It must consist of a maximum of 31 characters

20 First steps in Fortran 90 programming

In Fortran 90 names and keywords, upper and lower case letters are treated as identical, but can be used by the programmer to assist in the readability of the program. In this book we shall use upper case for all words which have a special meaning in Fortran, known as keywords, and will use lower case for names created by the programmer.

The name of the program should be chosen to indicate what the program does and should be different from any other names used for other purposes elsewhere in the program.

Note also that the blank (or space) between the word PROGRAM and the name circle is included, as in normal English, to make the program easier to read.

There may be any number of blanks between successive words in a Fortran 90 statement, as long as there is at least one, but they will be treated as though there was only one by the compiler when it is analysing the program. It is not necessary to include blanks between successive items in a list separated by commas or other punctuation characters, although they may be included if desired to make the program easier to read. However, it is not permitted to include spaces within a Fortran keyword or a user-specified name, except when using the older fixed form style of programming which is described in Section 2.6.

IMPLICIT NONE

This is a special statement which is used to inhibit a particularly undesir-able feature of Fortran which is carried over from earlier versions of Fortran. We shall explain its meaning in full in Chapter 3; for the present we shall merely state that it should always be placed immediately after the PROGRAM statement.

This program calculates the equation of a circle passing through three points

These two lines are comments.

A comment is a line, or part of a line, which is included purely for information for the programmer or anyone else reading the program; it is ignored by the compiler. A comment line is a line whose first non-blank character is an exclamation mark, !; alternatively, a comment, preceded by an exclamation mark, may follow any Fortran statement or statements on a line, as can be seen later in this program. We shall normally use comment lines in example programs, but will also use trailing comments where these are more appropriate.

You should always use comments liberally in your programs to explain anything which is not obvious from the code itself. You should always err on the side of caution, since what is clear to you may not be clear to someone else who has to read your program. Indeed, it may not even be clear to you six months after the code was written!

! Variable declarations REAL :: xl,yl,x2,y2,x3,y3,a,b,r

Some basic Fortran 90 concepts 21 The first of these lines is a comment which indicates that the line

following contains one or more variable declarations. It is not obligatory, but such comments help a reader to follow the program more easily.

The next line is a specification statement and provides important information about the program to the compiler. In this case it specifies that the nine names xl, y1, ... , r are the names of variables which will be used to hold numeric information. As we shall see in Chapter 3, there are several ways in which numeric information may be stored in a computer, but the most common type is known as a real number. We shall examine the detailed syntax of variable declarations in Chapter 3.

! Step 1

PRINT *,"Please type the coordinates of three points"

PRINT *,"in the order x1,y1,x2,y2,x3,y3"

The next block of statements is preceded by the comment Step 1, simply to indicate that these statements correspond to step 1of our structure plan.

The following two statements are the first statements to be obeyed during the execution of the program, and are called executable statements. These particular executable statements are known as list-directed output statements and will cause the text contained between the quotation marks (or quotes) to be displayed on your computer's default output device, probably the screen. We shall examine the way in which these statements work in Chapter 3.

READ *,x1,y1,x2,y2,x3,y3 ! Read the three points

This statement is clearly closely related to the previous two statements and has a very similar structure. It is called a list-directed input statement and will read information from the keyboard, or other default input device. It will be discussed in detail in Chapter 3. Note the use of a trailing comment.

! Step 2

CALL calculate_circle(x1,y1,x2,y2,x3,y3,a,b,r)

We now move on to step 2 of the structure plan. The CALL statement causes the processing of the main program unit to be interrupted and processing to continue with the procedure, or subroutine, whose name is given in the statement. Thus, as we anticipated in our structure plan, we do not need to know at this stage (or perhaps ever, if someone else writes it!) how the procedure will calculate the coefficients of the equation which defines the required circle. The items enclosed in parentheses following the procedure name are known as arguments and are used to transmit information between the main program and the procedure; in this case the relevant information required by the procedure is the coordinates supplied by the user, while the information returned by the procedure will be the coordinates of the centre of the circle and its radius. We shall investigate the way in which procedures are used and written in Chapter 4,

22 First steps in Fortran 90 programming

as soon as we have learned about the various types of data that Fortran can process.

! Step 3

PRINT *,"The centre of the circle through these points is &

'(" ,a,", II,b, ")n

PRINT *,"Its radius is ",r

Step 3of the structure plan relates to the display of the required results.

The first PRINT statement, however, incorporates a new concept - that of a continuation line. If the last non-blank character of a line is an ampersand, &,

then this is an indication that the statement is continued on the next line.

There are two cases here. If, as in this case, the ampersand occurs in a character context, that is, in the middle of a character string enclosed

in

quotation marks (or one enclosed in apostrophes), then the first non-blank character on the next line must also be an ampersand, and the character string continues from the character after that ampersand. Thus the two lines above are identical to the single line

PRINT *,"The centre of the circle through these points is (",a,",",b,")"

(where the smaller type is used here solely to fit the whole statement on a single line).

The other situation is where the first ampersand does not occur within a character string enclosed in quotes or apostrophes. In this case there are two possibilities. The first is that, as in the character string case, the first non-blank character of the next line is an ampersand, in which case the effect is just as before. However if the first non-blank character on the next line is not an ampersand then the effect is as if the whole of that line follows the previous one (excluding the ampersand). Thus, for example, the statement discussed earlier, representing step 2of the structure plan could also be written

CALL calculate_circle(xl,yl,x2,y2,x3,y3,&

&a,b,r)

which would be identical, as far as the compiler was concerned, with the original version, or

CALL calculate_circle(xl,yl,x2,y2,x3,y3,&

a,b,r)

which would be treated as though there were a number of spaces before a, although, as those spaces come between items in the list of arguments, they do not matter.

These two PRINT statements are different from the earlier ones in that they will print variable information as well as constant character strings. It should

Some basic Fortran 90 concepts 23 be obvious to the reader that they will print, or display, the appropriate text

followed by the value of the specified variable or variables, as calculated by the procedure calculate3ircle. This extended use will be described in detail in Chapter 3.

END PROGRAM circle

The final statement of a program must be an END statement. In this context it can take three forms:

END

END PROGRAM

or

END PROGRAM name

where name, if present, must be the same as the name on the corresponding PROGRAM statement. In general it is both good practice, and makes the program easier to follow, to use the third, full, form of the statement, as we shall do in all the examples in this book.

As might be expected, execution of the END statement brings the execution of the program to an end, and control is returned to the computer's operating system.

The overall structure of a Fortran 90 main program unit is shown in Figure 2.2.

PROGRAM name

Specification statements

Executable statements

END PROGRAM name

Figure 2.2 The structure of a main program unit.

24 First'steps in Fortran 90 programming