B side: Fortran, the language that speaks maths

Fortran lets you write maths in a form that a machine can run. The name comes from FORmula TRANslation. A program called the compiler translates your formulas into the machine tongue.
A long history, one language
Section titled “A long history, one language”Fortran started in 1957. It is one of the oldest languages that people still use. It changed over time, as English changed from the time of Shakespeare to today.
| Dialect | Year | What it added |
|---|---|---|
| FORTRAN 77 | 1978 | The form of old programs: fixed columns, capital letters |
| Fortran 90 | 1991 | Free-form text, arithmetic on full arrays, modules |
| Fortran 2008 | 2010 | Coarrays for parallel work, DO CONCURRENT |
| Fortran 2018 and 2023 | 2018, 2023 | More parallel features and more built-in procedures |
We learn modern Fortran (2008 to 2023). The old FORTRAN 77 dialect does not have many of the words on this site.
The parts of speech on the B side
Section titled “The parts of speech on the B side”- Verbs are statements that do something.
PRINTshows values.CALLruns a subroutine.ALLOCATEmakes space for an array. - Nouns are types of value.
INTEGERis a whole number.REALis a number with a decimal point.CHARACTERis text. - Adjectives are attributes. They describe a noun.
PARAMETERmeans “this value never changes”.ALLOCATABLEmeans “the program sets the size when it runs”. - Adverbs change a verb.
CONCURRENTinDO CONCURRENTtells the compiler that the steps can happen in any order. - Idioms are built-in procedures: ready-made maths phrases.
SQRTgives a square root.SUMadds a full list.MATMULmultiplies two tables of numbers. - Grammar marks are the maths signs:
+ - * / **. The=sign means “put the value on the right into the name on the left”. - Notes to the translator are structure words.
PROGRAM,MODULEandCONTAINSgive the compiler the structure of the text.
The shape of one sentence
Section titled “The shape of one sentence”A line of Fortran looks like a line of maths:
energy = 0.5 * mass * speed**2 ! kinetic energy| Part | In the example | Meaning |
|---|---|---|
| Name | energy |
The noun that gets the result |
| Assignment mark | = |
Put the result into the name |
| Formula | 0.5 * mass * speed**2 |
Maths, written almost as on paper |
| Comment | ! kinetic energy |
A note for people. The compiler ignores it. |
What the B side says well
Section titled “What the B side says well”- You write formulas as maths.
- A full array is one noun. One line can do arithmetic on thousands of values.
- The compiler finds many mistakes before the program runs.
- The same text works on many different machines.
What the B side cannot say easily
Section titled “What the B side cannot say easily”- Fortran has no words for the screen, the keyboard chip or sound.
- To control the hardware, Fortran needs help from C or from Assembly.
Fortran has no reserved words
Section titled “Fortran has no reserved words”In most programming languages, a keyword cannot be a name. Fortran is different. It has no reserved words. You can name a variable real or if. This is legal, but it makes the text hard to read. Do not do it.
The segments
Section titled “The segments”Each segment below has a B page. Open a segment to see its strengths and its words.
- Keeping values: How do you tell the machine to remember a number?
- Doing arithmetic: How do you tell the machine to calculate?
- Making choices and repeating: How do you say “if” and “again”?
- Working with many values: How do you do one thing to a long list of values?
- Organizing a program: How do you divide a long text into parts with names?
- Showing text and reading keys: How does the program talk to the person at the keyboard?
- Controlling the hardware: How do you talk to the screen, the keyboard chip and the clock directly?