This form is probably one that any beginning programmers will understand the easiest. To read these numbers into program variables, add them together, and finally print the result, the program would take on the following structure:
program add c c This program takes two numbers and adds them together c implicit none real a,b,s c a - one of the numbers to be added c b - the other number in the sum c s - the sum of a and b c c Get the numbers from the program user c First ask for the numbers c print *, ' This program adds 2 real numbers' print *, ' Type them in now separated by a comma or space' c c Now read the numbers that are typed by the user c this Fortran read will wait until the numbers are typed c read *, a,b c c Now calculate the sum c s = a + b c c Print out the results with a description c print *, ' The sum of ', a,' and ' , b print *, ' is ' , s stop end
In this format, the first line," program add", is called the program card and it just gives your program a name. It is not mandatory in Fortran, but good form. The next three lines begin with a 'c' and are called comment lines. These are used here and elsewhere to describe what is going on inside the program. The comment lines are not needed by the compiler, as it will ignore them. However, they are very necessary for you as the programmer. These lines will help you keep track of variables when you are writing a large program or allow you to come back months or years later and be able to understand quickly what is happening in the program.
The next statement," implicit none", tells the compiler to ignore the old Fortran standard that automatically assumes any variables beginning with the letters i,j,k,l,m or n are integers while any others are real numbers. You could be asking for trouble if you exclude the IMPLICIT NONE statement and allow the computer to make the above mentioned assumptions. The primary reason for this statement is to force you to make a list in type statements (REAL, INTEGER, ...) of all acceptable variables in the program. Then if you mistype a variable name (say TALBE instead of TABLE), the compiler will immediately detect your error and flag it. Another reason to avoid default variable types is to avoid using a name with the intent of using one variable type, and getting another. If you let the default variable type assumptions stand, you might use some variable like "ldata", intending it to be a real number. However since it begins with the letter l, Fortran will automatically assume it to be an integer. This will most likely give you some very unacceptable error in your calculations. Therefore, always use the IMPLICIT NONE statement.
Now, the next line is a direct consequence of using the IMPLICIT NONE statement. Since you told the computer not to assume any variable types, you need to tell it what types to associate with what variables. After the a few more comments comes the print statement. It will prompt the user by printing the message between the single quotes (double quotes work too with Fortran 90) on the monitor screen. The "*" in the print statement is called a "list-directed" format specification. It tells the computer to use its favorite format for printing the information that follows. A comma must always follow the "*".
The read statement is the next executable Fortran statement. This statement will pick up the numbers the user types. In this statement the "*" tells the computer to do its best to interpret what is typed, based upon the data types of the variable names that follow. The computer is being asked to take the first number typed and place it in the memory location represented by the name "a", in floating point format, and to place the second number typed in the memory location represented by the name "b", also in floating point format. To distinguish between the two typed numbers, the resulting program will require you to separate them with a space or a comma as you type them. When you are finished typing the numbers, you must hit the "Return" ("Enter") key before the program will actually pick them up. If you hit the "Return" key after entering the first number, the program will wait until you enter the second number and hit the "Return" again before using the numbers.
Now comes the arithmetic assignment statement, s = a + b. One thing that you should note about the line is that the equals sign is a little misleading. This isn't the type of equation that you are used to from algebra. If you have values for "a" and "s", the computer won't automatically solve the equation to get a value for "b". What the statement says is "Take the value associated with 'a', add it to the value associated with 'b', and store the result in the memory location represented by 's'. The only thing permitted to the left of an "=" in a Fortran statement is some variable name that gives a location to receive the results of the operations on the right of the "=".
The last thing left in our program description is to print the results. In most instances, it is a good idea to include in your results a reflection of the original input, to let the user know that the program is working with the proper numbers.
Notice how the first print is structured to produce a combination of some character strings and values of the variables "a" and "b". Get used to the way commas are used to separate items to be printed. Also notice when you run this sample program that the second print starts its output to the screen on a new line. Every print statement starts its output at the beginning of a new line on the screen.
We have finished the program as originally stated, but need to do a little housekeeping before we are done. First we include a statement that will be translated by the compiler into some machine instructions telling the program to STOP execution and return control to the operating system. This statement is actually not mandatory at this position in the code. The following END statement not only marks the end of the program unit, but will result in generation of machine code to stop execution when located in the main program. The END statement must always be present to mark the end of a program unit for proper processing by the compiler.
Authored by John Mahaffy and Jason Wehr Maintained by John Mahaffy : email@example.com