Program in Fortran

Many people perceive Fortran as an archaic and "dead" programming language. However, most scientific and engineering code is written in Fortran. As such, programming in F77 and F90 remains a necessary skill for most technical programmers. Moreover, the latest Fortran standards (2003, 2008, 2015) allow the programmer to write highly efficient code with minimum effort, while utilizing all of the modern language features, such as OOP (object-oriented programming). FORTRAN is an acronym for " formula TRANslation", and is best suited for mathematical and numerical applications rather than graphics or database applications. Most fortran codes take text input from a file or command-line rather than from a menu or GUI interface.

Steps

  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.
  2. If you already know another programming language just start learning the syntax and looking up equivalents to various commands. Otherwise, you should do this:
  3. Learn how to compile and run a basic program, this will be your first program, typically it will just print "Hello World" to the screen and exit. Don't worry about all the minor details of the syntax, just become comfortable with compiling and running.
  4. Learn about variable types (INTEGER, REAL, CHARACTER, LOGICAL)
  5. Learn about the concept of variables, arrays, functions, and subroutines. Variables are where information is stored, functions and subroutines are pieces of code that can be executed, and arrays are groups of variables.
  6. Learn conditional statements, such as the "IF" and "SELECT CASE" statements. 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).
  7. Learn loops and the "EXIT" and "CYCLE" statements.
  8. Learn about SUBROUTINES and FUNCTIONS.
  9. Learn Recursion (F90 and later) and other advanced topics
  10. Read or look up some books on Scientific Programming. 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.
    • 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(*,*) 'Hello World'
    • END PROGRAM
      • Write(*,*) means "write some unformatted text to standard output"
      • Why the spaces? 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"
  1. Remember that Fortran 90 introduced the "Free Form" source code, allowing code to be written without the spaces and without the 72 character limit. 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

Compiling

Once you've written your code, you need to compile it. This means translating the text file (which you can read) to a set of objects and executable files the computer can read. Let's say you're writing your killer application, named killerap.x and your code is in two files, main.f and morestuff.f.

  1. Compile the two fortran files:. 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.
  2. Speed up your code by writing efficient code. 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.

Tips

  • Start with small programs. When you are making your own code, try to identify the most essential part of the problem - is it the data input or the calling of the functions, the structure of the loop (these are some very elementary examples) and start from there. Then build upon that in small increments.
  • EMACS is a good free text editor to use instead of Notepad
  • G77 and GFORTRAN are two free FORTRAN compilers
  • You might find it easier to use an online IDE (integrated development environment) at first. A good option is Coding Ground.[1] You will find a multitude of programming languages there, including Fortran-95. Another option is Ideone.

Things You'll Need

  • A FORTRAN compiler. There are FORTRAN compilers for Windows, Mac OS, and Linux.
  • A text editor. Most (All?) operating systems come with text editors; however, you may prefer some other text editor over the default.

Related Articles

Sources and Citations

  1. tutorialspoint.com/codingground.htm

You may like