Your first Fortran program
Why a language designed in the 1950s still runs the world's weather and physics code, and the smallest complete program: a program block, implicit none, and one print statement.
The oldest language still doing new work
Fortran was designed in the 1950s for one job: turning formulas into fast machine code. It won that job so completely that weather models, aircraft simulations, climate research and physics codes are still written in it today. Very few languages have been in continuous use for as long, and none of them are as easy to find at the centre of a numerical code.
The name is short for Formula Translation, and the language has changed enormously since 1957. What you write here is modern Fortran, in free form, in a file ending in .f90 — no punched cards, no fixed columns, and code that reads much more like the formula than like the machine.
The shape of every program
A program is a block. It starts at program name and ends at end program name, and the statements between them run in order, top to bottom. This site runs a single file, so every example is one main.f90 holding one program.
implicit none is the first line inside the block, and it is not optional here. Without it, Fortran gives a type to every name you have not declared by looking at its first letter — the rule from 1957 that names beginning with i through n are integers. With it, every name must be declared, a typo becomes a compiler error instead of a wrong answer, and the compiler can tell you what you did rather than guess what you meant.
Comments begin with ! and run to the end of the line. There are no semicolons and no braces: line breaks are the structure, which is why Fortran reads line by line like a specification rather than like a script.
The name on the program line is not decoration: end program name repeats it, and the compiler checks that the two agree. A program block can also hold internal procedures after contains, which is how chapter 6 keeps a helper in the same file.
The smallest program there is
program hello
implicit none
print *, 'Hello from Fortran!'
end program helloPrinting a line
print *, ... writes the values after the star, separated by commas. The * means "you choose the layout", which Fortran calls list-directed output: the compiler picks a sensible width for each type and pads it.
That convenience has a visible cost. Every line starts with a blank and character values print back to back, but numbers sit in wide fixed fields — an integer gets twelve columns — so a label and its number end up far apart. Chapter 8 replaces this with formats that put every value exactly where you want it.
print always writes to the default output unit, which is the terminal here. write(*, *) is the same operation with the unit spelled out: the first star means the default unit and the second means list-directed formatting. You will see both forms in older code; in this course print is enough until a format is needed.
What the compiler actually does
Before anything runs, the compiler reads the whole file, checks that every statement is legal, and translates it into machine instructions. A program with one mistake anywhere does not run at all: there is no half-compiled state and no “the lines before the error worked”. That strictness is why a misspelled name is a message from the compiler rather than a wrong number at run time.
The extension matters. A .f90 file is free form: a statement starts wherever you put it, indentation is only for the reader, and a line may be up to 132 characters. The older .f and .for forms are fixed form: the first six columns hold labels and continuation marks, and a character in column one can mark a comment. Ordinary-looking code in those files can fail with strange errors, so check the extension first.
Three slips catch almost every beginner. Leave out implicit none and a misspelled name becomes a new variable instead of an error. Break a long print across two lines without the trailing & and the second line is read as a new statement. And resist the braces and semicolons of other languages: braces are syntax errors here, and a semicolon separates statements rather than ending one.
Try these next
- Change the greeting and add a third
printline of your own. - Print
2 + 3 * 4,(2 + 3) * 4and2 ** 10, and predict each answer before running. - Write three lines and a row of dashes, then delete the
end programline and read the error.
Run it, then make it yours
Press Run to compile and print. Then change the greeting, change the arithmetic to 6 * 8, and run again — watch which column the number lands in.
A banner with arithmetic
Run it and check that the three numbers line up. Then change one label so it has a different length, run again, and watch the column break.
Change a string and press Run again. The run goes to a real Fortran compiler on the server, so the first one may take a second; after that the round trip is quick.
Check what you learned
One question at a time. A wrong answer explains itself — take another go.
Part 1 of 5
What does implicit none do?
