next up previous
Next: Compiling and running the Up: Introduction to Fortran 77 Previous: F77 indentation rules

The first F77 program

Before we begin writing our first source code in Fortran 77, it will be very useful for you to make a special directory which will accommodate your all your Fortran codes written during this course. This can be done by typing the following command in the command-line terminal:

Image win_logo.gif

mkdir c:\work
cd c:\work

Image mac_logo.gif

mkdir work
cd work

The second command moves you to the directory you just created.

Assume that you have to write a program which calculates the factorial of a particular number. To start writing your code, open your command-line terminal and run emacs. In the main window, press C-x C-f, i.e. press and hold Ctrl and press x and then f. In the bottom part of your emacs window, you should now see the following prompt:

  Find file:  c:\work\
or something like /Users/name/work/ on MacOS X, where name is your user name. You are now expected to enter the file name of the program you are going to write. I recommend you to always think a few seconds before you decide about the name, because a convenient file name can always help you to find the source code you seek. In our case, a good idea is to choose the name fact.f. The .f at the end should be always added, because: (i) it helps you to recognize which files contain Fortran codes, and (ii) once you use the extension .f, your Emacs editor automatically switches to its Fortran mode and turns on highlighting the Fortran syntax. After typing the file name, the bottom line of your screen should look as follows:
  Find file:  c:\work\fact.f
or slightly differently on Mac. Now, press Enter and Emacs opens an empty buffer for editing your file fact.f. The status line at the bottom of your Emacs buffer should now read:
  --:--  fact.f      (Fortran)--L1--All----------------------------------------
which means that the file name assigned to the current buffer is fact.f. Because the extension of our file is .f, Emacs automatically recognized that we are going to write a Fortran code and switched to the Fortran mode.

Following is the program for the calculation of the factorial which we are going to type. The numbers and dashes above the code do not belong to the program and they merely serve as a ``ruler'' which helps you to recognize different columns. Do not type this line !

12--567------------------------------------------------------------73
program FACTORIAL

! definition of variables
integer i, res, number
parameter( number=5 )

res = 1
do i=2,number ! this loop calculates the factorial
res = res*i
enddo

write(*,'("Factorial of ",I3," is ",I5)')
+ number, res

end

We are now at the beginning of the buffer. Press Tab - you should see that the cursor jumps to column 7 at which the instruction part of your code starts. Then, write program FACTORIAL and press Enter. You are done with editing the first line of your source code.

The second line is inserted by simply pressing Enter. I strongly recommend you to leave a blank line each time you need to distinguish between independent parts of your code. In order to write a ``readable'' code, you should always separate the line with identifier program from variables (see below), variables from the instruction part of your code and this part from the line end.

The third line of the code above is the comment line. Although comments are disregarded by your Fortran compiler, they help you to organize your source code and also help the other people to understand what your code really does. To write a comment, put an exclamation mark (!) in column 1 and type your comment. You should see that Emacs understood that you are writing a comment line and displayed this line with a different color.

The subsequent block always contains the declaration of variables which are used locally within the scope of our program. In this domain, you attach to each variable a type which tells your compiler whether the variable contains an integral number (integer), real number (real), logical number (logical), alphanumeric text (character), etc. Here, we use only three variables named i, res and number. Press Tab at the beginning of each line. This moves the cursor to column 7 at which F77 expects the declaration of variables. If you write more than one variable on a line, they should always be separated by a comma (,). The following line assigns a value to variable number which is declared above; more on this shortly.

The instruction part is the real ``heart'' of your code which contains the algorithm determining the factorial of a particular number number. Each line is typed such that you first press Tab. The cursor moves to the column at which the Fortran expects your input. The first line of this block, res = 1, simply fills the variable res with number one. Press Enter and continue editing a new line. The do-enddo part is a loop which carries out the algebraic multiplication 1*2*3*$\dots{}$*number. The first line is do i=2,number which means that the loop is repeated for i$=2,3,\dots{},$number. Behind this header of the loop, we insert a comment which again starts with an exclamation mark (!). Pressing Tab at the beginning of the next line moves the cursor to column 10. Those three columns are added automatically to help you organize your Fortran program. Finish this line by typing res = res*i, which multiplies the contents of the variable res with the number i and stores the result in res. Finally, the tail of our loop is inserted by typing enddo in the subsequent line. This was a little longer part and, therefore, leave the next line blank.

Now, the part which calculates the factorial is typed. The only task left is to display the result. This is done by the instruction write. The asterisk (*) in the argument of this command means that the result will be printed by the terminal you are currently in. The second argument of write is the pattern which determines the style of output. If the command write were written entirely on one line, it would extend beyond column 73, which is not permitted. Therefore, we have to break the command and continue on the next line. Remember, that the symbol + is added in column 6 to designate that this line is a continuation of the command in the previous line. Finally, the write command displays Factorial of followed by the value of number, followed by is: and finished by the result of the multiplication stored in res.

The absolute end of your program is entered by writing end which is always the last instruction of your code. To save it, press C-x C-s, i.e. press Ctrl, keep holding it and press x and then s. You should see the following message in the bottom line of your Emacs editor:

  Wrote c:\work\fact.f
Congratulations, your first F77 code is born. Are you eager to see the result ? Continue in the next section.


next up previous
Next: Compiling and running the Up: Introduction to Fortran 77 Previous: F77 indentation rules
Roman Groger
2004-09-27