Modern Fortran
Third edition, supplemented and revised
1. Elements of language
1.1. Free form of writing a program
Consider a program that specifies two real numbers, their sum is calculated and the result is displayed:
program p1! p 1 - program name
real x, y, z! We declare 3 variables of real type x = 1.1! Assigned to the variable x and y values y = 2.2
z = x + y! Assigning to z the result of adding x and y print *, 'z =', z ! Displaying the result on the screen
! Output result: z = 3.300000
end program p1! END is a mandatory program termination statement
The given program is called the main one . It is built according to the scheme:
first is the declaration of the types of the variables used, then operators that execute on declared variables some
actions. This scheme is typical and has been reproduced several times in manuals.
The program ends with an END statement, in which the program name is p 1 and simultaneously PROGRAM p 1 can be omitted. In other words, the program can be ended like this: END PROGRAM or END. Program has the title: PROGRAM program-name . However, such a title can be omitted. In this case , program-name cannot be present in the END statement. The program-name must be different from names used within the main program.
The program is written in free form . By default the text file a free-form program has the extension F90. It does not there must be a directive $ NOFREEFORM, and when compiling it is impossible set the compiler option / 4Nf.
Comment. Hereinafter, compiler options inherent in FPS are indicated. Description of CVF compiler options and correspondence between options compilers FPS and CVF are described in [1].
In free form, the text of the program is written according to the rules:
the length of a line of text is 132 characters;
operator record can start from any position of the line;
several lines separated by a dot can be placed on one linesemicolon (;) operators;
if a line of text ends with a &, then the next linetreated as a continuation line;
O. V. Barteniev. Modern FORTRAN
the operator can contain up to 7200 characters. Continuation lines in free form cannot be more than 54;
any located between the exclamation mark and the end of the linesymbols are treated as a comment, for example:
real x, y, | & | ! Start line comment |
z, a (5), | & | ! Continuation line |
r, b (10) | ! One more continuation line |
x = 1.1; y = 2.2; a = -5.5 | ! Assignment operators |
Comment. In addition to a free program, you can write in a fixed form inherited from Fortran 77 (Appendix 2). Files, containing text in fixed form, by default have extensions F or FOR. In files with such extensions, you can go and to free form by specifying the $ FREEFORM directive or compiler option / 4Yf [1].
Let us now run the program p 1 using those given in Sec. 1.2 intelligence.
1.2. Console project
The program is considered as a project in FPS and CVF. To start a new the program must first of all create it. There are several types of projects, however, at first we will work with the console project - a one-window project without graphics.
1.2.1. Creating a project in CVF
Let's start creating a project by launching DS. To do this, after starting Windows 95 or Windows NT, you can perform a chain of actions: start -
Programs - Compaq Visual Fortran - Developer Studio. Let's move on to creating a new CVF console project. To do this, execute the File chain New - select the Projects tab - Fortarn Console Application - set a name project, for example proj1, - set the project location folder, for example
D: \ FORTRAN \ proj1, - OK. In the window that appears then select the button
Anempty project and click Finish. Then a directory (folder) will be created, whose name is the same as the project name. This folder will contain project files with extensions DSP, DSW and NCB. Also will be created
Debug folder. The project itself will be displayed on the FileView tab (Fig. 1.1).
1. Elements of language
Figure: 1.1. Project proj1
Now let's create a file in which the text of the program will be entered, by executing: File - New - select the Files tab - select Fortran Free Format
Source File - activate the Add to project option - set the file name, e.g. myp (extension omit) is OK. The generated file will receive extension F90 and will be located in D: \ FORTRAN \ proj1.
If the file already exists, then to add it to the project in the window FileView select the Source Files folder and execute: click on the right mouse button - Add Files to Folder - select the type of files and the necessary files (fig. 1.2) - OK.
Figure: 1.2. Adding files to the project
O. V. Barteniev. Modern FORTRAN
To display the contents of the file on the screen, just hit on it twice with the mouse.
1.2.2. Creating a project in FPS
FPS can be equipped with an earlier version of DS, in which the scheme creating a project and adding a file to it is somewhat different.
After starting DS, execute the chain File - New - Project Workspace -
OK - Console Application - enter project name - set location project on disk - Create. After clicking the Create button, a directory (folder), the name of which is the same as the name of the project. In this folder project files with MAK and MDP extensions will be placed.
Let's create a new file now by doing File - New - Text File - OK.
Next, type the text of the program in the right window and write it to disk: File -
Save - select the directory on the disk for recording the file - set the file name with an extension, for example myp.f90, - save.
Add the created file to the project: Insert - File Into Project - select file myp.f90 - Add.
1.2.3. Project operations
To close the project, run: File - Close Workspace.
An existing project is opened as a result of executing the File chain Open Workspace - select the project file - Open. To remove a file from of an open project, just select this file in the FileView window and press Del.
Now let's compile the project: Build - Compile - and fix it errors found, messages about which you will find in the lower window on the Build tab.
Let's create an executable EXE file: Build - Build. Let's run it for execution: Build - Execute - and get the result. To exit the work
DOS window, in which the results are displayed, press any key, for example Esc or Enter.
You can also compile, build and run the program by using the buttons available in the environment (Compile, Build, GO) or by choosing on the keyboard the corresponding keyboard shortcuts, information about which located in the DS menu items. Here we note that all three the above steps (compilation, build and run) will be done
after pressing Ctrl + F5. However, if there are several files in the project, it is more convenient
(from the standpoint of detecting errors) compile files separately, proceeding to processing the next file after eliminating all syntax errors in the current.
1. Elements of language
1.2.4. Source files