Lesson Topics | |
Editing Fortran | Assignment |
Compiling | Parameter |
Running a Program | Comments |
Program | Print * |
Variables | Read * |
Declarations | End |
Types | Operations |
Implicit Quantifier | Intrinsic Functions |
View Demos | Download Demos |
# 1 | # 1 |
Main Fortran Page |
The name Fortran originally referred to "Formula Translation", but it has long since taken on its own meaning. There are several versions of Fortran around, among them Fortran 77, Fortran 90, and Fortran 95. (The number denotes the year of introduction.) Fortran 77 is probably still the most used, and it is the version installed on UHUNIX and in the UH math lab. Even though this semester we have thus far studied Basic, at the same time we have studied Fortran, because commands and procedures are very similar in the two languages. Moving from QuickBasic to Fortran is more a matter of change of terminology than anything else.
A most peculiar feature of Fortran 77 is its line structure, which is a carryover from the old days when programs were typed on punch cards. A punch card had 80 columns, and so does a line of Fortran code. A "c" in column 1 indicates a comment (similar to REM in Basic). Columns 2-5 (usually left blank) are reserved for line numbers. Column 6 is used only to indicate a continuation of a line too long to fit on the card. Columns 7-72 contain the instructions of the program. Columns 73-80 were originally used for numbering the punch cards, but are rarely used nowadays - leave them blank and the compiler will ignore them.
Fortran is case insensitive - that is, it does not distinguish between capital and small letters. Thus x and X refer to the same variable. Many programmers for simplicity use all small letters, but you may do as you like. Also, after column six Fortran does not recognize spaces (except for spaces inside quotations as in print statements). In general, spaces are mostly for the purpose of making code more readable by humans. When you type a Fortran program with an editor, make certain the editor indents more than six spaces; then if you begin every line with an indent you do not have to worry about counting six spaces at the beginnings of lines.
Let us go through the steps of editing, compiling, and running a short program. First open Notepad under Windows, or type "edit" (and return) under a DOS prompt to open the DOS editor. (When you double-click the Fortran icon on a math lab computer, you get a DOS prompt.) Beginning each line with an indent (except for the fourth line, where the "c" must be placed in the first column), type the program exhibited below; the program computes the area of a circle of radius r, as input by the user. The resulting file that you save is called the source file for the program.
program circlearea | |
real r, area, pi | |
parameter (pi = 3.14159) | |
c | This program computes the area of a circle. |
print *, "What is the radius?" | |
read *, r | |
area = pi * r ** 2 | |
print *, "The area is", area | |
print *, "Bye!" | |
end |
The first statement above gives the program name, the second declares that "r", "area", and "pi" will be single precision real quantities, and the third announces that pi has the value 3.14159. The fourth statement, beginning with "c" in column 1, is a comment describing what the program does; such comments are for the benefit of the programmer and are ignored by Fortran. The fifth statement prompts the user for the radius of the circle, and the sixth accepts this input. The seventh statement computes the area and the eighth informs the user of this area. Finally, the last two statements bid goodbye and terminate the program.
The name for a source file in Fortran must end with the extension ".f" before the compiler recognizes it. After you have typed the above program, save the file as area.f. (If you type the file in Notepad, include the whole name in quotes when you save it, as otherwise the extension .txt will be added to the name.) The file will be saved to your h directory in the math lab. Under a DOS prompt you can view the files in this directory by typing dir and enter; under Windows you can double-click "My Computer" and then the icon for the h drive.
If your program has mistakes (which usually happens on the first attempt at compiling), instead of a compiled file you will get Fortran error messages pointing out problems. Some of these messages can be hard to decipher, but after reading hundreds of them you will get better at it. If your program has no mistakes Fortran will simply return a DOS prompt - that is good news because it means Fortran has successfully created a compiled file. By default this new file is given the name a.exe. (You can give the compiled file a name of your own choosing by typing g77 area.f -o name.exe to compile the program - but usually there is no reason not to accept the default name.) Your compiled file, also located in the h directory, is now executable - that means the program is ready to run.
If you do enough Fortran programming, sooner or later you will err and create and run a program that never stops. In such a situation, type "Control-C" to interrupt the execution of the program.
Now that we have discussed the basic nuts and bolts of creating and running a Fortran program, we discuss some terminology and commands. You will probably find that most of these remind you of similar things in Basic.
program quadratic |
program mortgage |
program primes . |
The variable types in Fortran are
1) integer (in the range from about - 2 billion to + 2 billion)As illustration, the declaration statements2) real (single precision real variable)
3) double precision (double precision real variable)
4) character (string variable)
5) complex (complex variable)
6) logical (logical variable)
real r, area |
integer M, N |
double precision a, b |
declare that r and area are single precision real variables, that M and N are integers, and that a and b are double precision real variables.
If you do not declare the type of a variable, Fortran will by default make it an integer if it starts with one of the letters i through n, and will make it a single precision real variable otherwise. However, it is normal (and good) programming practice to declare the type of every variable, as otherwise mistakes are easily made.
The implicit quantifier before a type declaration makes all variables starting with the listed letters of the specified type. For example, the declarations
implicit integer (i-m) |
implicit real (r-t) |
make variables starting with i, j, k, l, m integers, and those starting with r, s, t real. However, the implicit quantifier is probably best avoided, as programmers with short memories will make mistakes.
A declaration statement is nonexecutable - that is, it provides information but does not instruct Fortran to carry out any action. Declarations must appear before any executable statement (a statement that does tell Fortran to take some action).
where name is replaced by the name of the constant, and constant expression by an expression involving only constants. Thus
specifies a value for the constant pi, while the succeeding statement
fixes values of new constants a and b in terms of the old constant pi. Remember that once a constant is defined you are not allowed to change its value later.
All parameter statements must appear before the first executable statement.
print *, x |
print *, "The solution is ", x |
print *, 'The radius is', r, 'and the area is', area |
Note that a comma follows "print *", and that commas (instead of semicolons as in Basic) appear between successive items to be printed. Observe also that either double or single quotes may enclose strings. The command "print *" on a line by itself (without a comma) serves as a line feed.
read *, radius |
read *, A, B, C . |
In the first example the program pauses to allow the user to enter the radius. In the second example the user types the values of A, B, and C, separated by returns; alternatively, the user can type A, B, and C separated only by commas, and then one final return.
Addition | x + y |
Subtraction | x - y |
Multiplication | x * y |
Division | x / y |
Exponentiation | x ** y |
Fortran performs exponentiations first, then multiplications and divisions, and lastly additions and subtractions. (When in doubt, use parentheses!)
Be careful with division. If m and n are integers, then m/n is truncated to its integer part. Thus 3/4 is evaluated as 0, and 25/6 as 4. When working with constants rather than variables you can avoid this problem by using periods after integers. For example 3./4. is evaluated in the standard way as .75, as Fortran treats 3. and 4. as real variables rather than as integers.
Function | Description |
abs(x) | absolute value of x |
acos(x) | arccosine of x |
asin(x) | arcsine of x |
atan(x) | arctangent of x |
cos(x) | cosine of x |
cosh(x) | hyperbolic cosine of x |
dble(x) | converts x to double precision type |
exp(x) | exponential function of x (base e) |
log(x) | natural logarithm of x (base e) |
mod(n,m) | remainder when n is divided by m |
real(x) | converts x to real (single precision) type |
sign(x,y) | changes the sign of x to that of y |
sin(x) | sine of x |
sinh(x) | hyperbolic sine of x |
sqrt(x) | square root of x |
tan(x) | tangent of x |
tanh(x) | hyperbolic tangent of x |