How to Program in Fortran
Have a good idea of what your program will do., If you already know another programming language just start learning the syntax and looking up equivalents to various commands., Learn how to compile and run a basic program, this will be your first...
Step-by-Step Guide
-
Step 1: Have a good idea of what your program will do.
Think about what sort of data is needed as input, how to structure the output, and include some intermediate output so you can monitor the progress of your calculation.
This will be very useful if you know your calculation will run for a long time or involves multiple complicated steps. , Otherwise, you should do this: , Don't worry about all the minor details of the syntax, just become comfortable with compiling and running. ,, Variables are where information is stored, functions and subroutines are pieces of code that can be executed, and arrays are groups of variables. , The "IF" statement will be one of your most frequently used statements, you can execute code based on whether a condition is true or not (e.g. whether the color the user provided was red). ,,,, For example, the book "Numerical Recipes in Fortran" is both a good text on scientific programming algorithms and a good introduction to how to put together codes.
More recent editions include chapters on how to program in a mixed-language environment and parallel programming.
Another example is "Modern Fortran in Practice" written by Arjen Markus.
The book gives an insight into how to write Fortran programs in twenty-first-century style in accordance with the latest Fortran standards., This is a relic from the past.
In the early days of computer, there were no terminals.
You entered data via punch-cards.
A "C" in the first line on the punch card indicated a comment line.
The next 4 spaces were reserved for line numbers and the 6th space indicated that the current line is a continuation of the previous line.
This is needed because punch cards were only 80 characters wide.
So, any line longer than 72 characters would be chopped off! Because of this legacy, lines of fortran are sometimes called "Cards"
For example, "Hello, World" could be written as  ! HELLO.F90
-- HELLO WORLD PROGRAM Â ! PUBLIC DOMAIN PROGRAM HELLO WRITE (*, *) 'Hello, World!'Â ! Display 'Hello, World' END PROGRAM Fortran , Typically, this is done by issuing the commands (the > indicates the command line prompt) >f77
-c main.f and >f77
-c morestuff.f which generates the files: main.o and morestuff.o then f77
-o killerap.x main.o morestuff.o which links main.o and morestuff.o to make your killerap.x.
The
-c and
-o flags are pretty common...but you should read up on compilation flags for your particular compiler. -
Step 2: If you already know another programming language just start learning the syntax and looking up equivalents to various commands.
However, most compilers include optimization algorithms that improve things even more.
These are typically turned on by including a
-O ,
-O2, or
-O3 flag when compiling (again depending upon your version of fortran).
Generally, the lowest level
-O or
-O2 level is best.
Be aware that using the more aggressive optimization option can introduce errors in complex codes and may even slow things down! TEST YOUR CODE. -
Step 3: Learn how to compile and run a basic program
-
Step 4: this will be your first program
-
Step 5: typically it will just print "Hello World" to the screen and exit.
-
Step 6: Learn about variable types (INTEGER
-
Step 7: CHARACTER
-
Step 8: LOGICAL)
-
Step 9: Learn about the concept of variables
-
Step 10: arrays
-
Step 11: functions
-
Step 12: and subroutines.
-
Step 13: Learn conditional statements
-
Step 14: such as the "IF" and "SELECT CASE" statements.
-
Step 15: Learn loops and the "EXIT" and "CYCLE" statements.
-
Step 16: Learn about SUBROUTINES and FUNCTIONS.
-
Step 17: Learn Recursion (F90 and later) and other advanced topics
-
Step 18: Read or look up some books on Scientific Programming.
-
Step 19: A simple "Hello World" Code: Note: you have to space each line over 7 spaces C HELLO.F -- HELLO WORLD PROGRAM C PUBLIC DOMAIN PROGRAM HELLO WRITE(*
-
Step 20: *) 'Hello World' END PROGRAM Write(*
-
Step 21: *) means "write some unformatted text to standard output" Why the spaces?
-
Step 22: Remember that Fortran 90 introduced the "Free Form" source code
-
Step 23: allowing code to be written without the spaces and without the 72 character limit.
-
Step 24: Compile the two fortran files: .
-
Step 25: Speed up your code by writing efficient code.
Detailed Guide
Think about what sort of data is needed as input, how to structure the output, and include some intermediate output so you can monitor the progress of your calculation.
This will be very useful if you know your calculation will run for a long time or involves multiple complicated steps. , Otherwise, you should do this: , Don't worry about all the minor details of the syntax, just become comfortable with compiling and running. ,, Variables are where information is stored, functions and subroutines are pieces of code that can be executed, and arrays are groups of variables. , The "IF" statement will be one of your most frequently used statements, you can execute code based on whether a condition is true or not (e.g. whether the color the user provided was red). ,,,, For example, the book "Numerical Recipes in Fortran" is both a good text on scientific programming algorithms and a good introduction to how to put together codes.
More recent editions include chapters on how to program in a mixed-language environment and parallel programming.
Another example is "Modern Fortran in Practice" written by Arjen Markus.
The book gives an insight into how to write Fortran programs in twenty-first-century style in accordance with the latest Fortran standards., This is a relic from the past.
In the early days of computer, there were no terminals.
You entered data via punch-cards.
A "C" in the first line on the punch card indicated a comment line.
The next 4 spaces were reserved for line numbers and the 6th space indicated that the current line is a continuation of the previous line.
This is needed because punch cards were only 80 characters wide.
So, any line longer than 72 characters would be chopped off! Because of this legacy, lines of fortran are sometimes called "Cards"
For example, "Hello, World" could be written as  ! HELLO.F90
-- HELLO WORLD PROGRAM Â ! PUBLIC DOMAIN PROGRAM HELLO WRITE (*, *) 'Hello, World!'Â ! Display 'Hello, World' END PROGRAM Fortran , Typically, this is done by issuing the commands (the > indicates the command line prompt) >f77
-c main.f and >f77
-c morestuff.f which generates the files: main.o and morestuff.o then f77
-o killerap.x main.o morestuff.o which links main.o and morestuff.o to make your killerap.x.
The
-c and
-o flags are pretty common...but you should read up on compilation flags for your particular compiler.
However, most compilers include optimization algorithms that improve things even more.
These are typically turned on by including a
-O ,
-O2, or
-O3 flag when compiling (again depending upon your version of fortran).
Generally, the lowest level
-O or
-O2 level is best.
Be aware that using the more aggressive optimization option can introduce errors in complex codes and may even slow things down! TEST YOUR CODE.
About the Author
George Jones
Experienced content creator specializing in creative arts guides and tutorials.
Rate This Guide
How helpful was this guide? Click to rate: