# fortran lesson

Post on 10-Apr-2015

532 views

Category:

## Documents

Embed Size (px)

TRANSCRIPT

FORTRAN LESSONLesson Topics Editing Fortran Compiling Assignment Parameter

Running a Program Comments Program Variables Declarations Types Implicit Quantifier View Demos #1 Main Fortran Page Print * Read * End Operations Intrinsic Functions Download Demos #1

IntroductionFortran is one of the oldest programming languages devised, but it is also still one of the most popular, especially among engineers and applied scientists. It was developed in the 1950's at IBM. Part of the reason for Fortran's durability is that it is particularly wellsuited for mathematical programming; moreover, there are millions of useful programs written in Fortran, created at considerable time and expense, and understandably people are reluctant to trash these old programs and switch to a new programming language. 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.

Editing Fortran

c

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.

CompilingAfter you have created and saved a source file, you next must compile this file. Open a Fortran window and enter g77 name.f, where in place of name you insert the name of your source file. (If the source file resides in a directory different from that of the Fortran program, you will have to include also the directory path of the file.) To compile the file of our example above, in the math computer lab you just enter g77 area.f. 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.

Running a ProgramIf your compiled file has the default name a.exe, you simply type a and return to run it (or name and return if you gave the file another name). After you run the program and see how it works, you can return to your editor and revise it as you wish. It is perhaps better to keep two windows open - both the Fortran window and the editing window - so that you can quickly switch from one to the other with a mouse-click. After revising a program, you must save and compile it again before changes take effect.

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.

ProgramEvery Fortran program must begin with a program line, giving the name of the program. Here are examples: program quadratic program mortgage program primes .

Variables, Declarations, TypesAfter the program name come the declaration statements, stating the types of the variables used in the program. A variable name consists of characters chosen from the letters a-z and the digits 0-9; the first character of the name must be a letter. You are not allowed to use your program name as a variable, nor are you allowed to use words reserved for the Fortran language, such as "program", "real", "end", etc. The variable types in Fortran are 1) integer (in the range from about - 2 billion to + 2 billion) 2) real (single precision real variable) 3) double precision (double precision real variable) 4) character (string variable) 5) complex (complex variable) 6) logical (logical variable) As illustration, the declaration statements 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).

AssignmentThe equals sign "=" assigns the variable on the left side the value of the number or expression on the right side (exactly as in Basic).

ParameterThe parameter statement works like CONST in Basic - it specifies a value for a constant. The syntax is parameter (name = constant expression) where name is replaced by the name of the constant, and constant expression by an expression involving only constants. T

Recommended