Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Standard view
Full view
of .
Save to My Library
Look up keyword
Like this
0 of .
Results for:
No results containing your search query
P. 1
Fortran Lesson

Fortran Lesson

Ratings: (0)|Views: 513 |Likes:
Published by api-3745065

More info:

Published by: api-3745065 on Oct 17, 2008
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less





Lesson Topics
Editing Fortran

Running a Program Comments

Print *
Read *
Implicit Quantifier Intrinsic Functions
View Demos
Download Demos
# 1
# 1
Main Fortran Page

Fortran 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 well- suited 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

Unlike in Basic, a Fortran program is not typed in a "Fortran window". Instead, a
program is typed and saved with anedi tor (i.e., a word processor), and the program is
then turned into an executable file by a Fortrancompiler. To begin the process of creating
a Fortran program in the math lab, you must open an editor. It is preferable to use a
simple editor - such as Notepad or the DOS editor - because fancy word processors might
add extraneous formatting notation that will hang up Fortran.

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)


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!"

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 typingdir and enter; under Windows you can double-click "My Computer" and then the icon for the h drive.


After 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 ofname 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 namea.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 nowexecu table - that means the program is ready to run.

Running a Program
If your compiled file has the default namea.ex e, 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.

Activity (32)

You've already reviewed this. Edit your review.
1 hundred reads
1 thousand reads
闕立凱 liked this
vijayang liked this
vijayang liked this
prasenajita2618 liked this
Murat Atiş liked this
khipasi liked this
treper23 liked this
khipasi liked this

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->