Chapter 2 — Teaching Notes

One-line purpose. Get every student a working compiler, their first compiled-and-run program, and a correct mental model of the compile–link–run pipeline — and plant the two habits (implicit none and the development flag set) the rest of the course silently assumes.

Key ideas to emphasize

  • The pipeline, not just the command. The single highest-value idea is that building is compile → link → run, and that "no IMPLICIT type" (compile) and "undefined reference" (link) fail at different stages with different fixes. Students who internalize this debug an order of magnitude faster. Draw the diagram on the board and refer back to it whenever an error appears.
  • implicit none is mandatory, and here is why. Do not present it as a rule to obey; present the bug it prevents (the veloctiy/celcius typo that silently becomes a zero-valued variable) and let the horror land. Case Study 1, Phase 3–4, is the dramatization; it is the emotional core of the chapter.
  • The two build profiles. Develop with -Wall -g -fcheck=all; run with -O2. The discipline "never time a -fcheck=all build" preempts a benchmarking mistake students make for years otherwise.
  • Compiled vs interpreted. The Python comparison is the honest framing: Python wins for four lines, Fortran wins when the loop is huge and repeated. Say it plainly; it builds credibility for later.

Misconceptions to preempt

  • "It compiled, so it's correct." (The compiler checks grammar and declared types, not intent — Case Study 1 runs and prints a wrong answer.)
  • "A warning is basically an error / warnings can be ignored." (Neither: it doesn't stop the build, and you should still fix it.)
  • ".f90 means the code must be Fortran 90." (No — .f90 means free-form; we write 2018 code in .f90.)
  • "implicit none is a style choice." (It is a bug detector; without it, a typo is a silent wrong answer.)
  • "gfortran on Windows makes .obj files." (No — gfortran makes .o everywhere; .obj is MSVC/Intel-Windows.)
  • "print output should be flush-left." (print * legitimately adds a leading blank; print '(a)' does not.)

A live demonstration (5–8 minutes)

Type hello.f90 live and build it wrong on purpose first: omit implicit none, and misspell a variable in a one-line computation (the celsius/celcius bug). Show it compiling and printing a wrong answer with no complaint. Then add implicit none and rebuild in front of them — watch the compiler catch the typo at a named line and column. Nothing sells the habit like seeing the silent bug become a loud error in real time. (Follow with the -c two-step so they see the .o appear, then relink.)

Class-time budget (~50 min)

  • 10 min: install/verify together; get every student to a working gfortran --version (do this FIRST — it is the gating step; stragglers pair with neighbors).
  • 8 min: hello.f90, both print forms, the leading-blank subtlety (§2.2).
  • 12 min: the compile–link–run pipeline with the -c two-step and the compile-vs-link error distinction (§2.3).
  • 8 min: the five flags and the two profiles (§2.4).
  • 8 min: implicit none with the live typo demo (§2.6).
  • 4 min: the first computation and launch of program heat (§2.7 + Project Checkpoint).

Prerequisites to review

Terminal basics: opening a shell, cd, ls, running ./program. On Windows, decide as a class between WSL and MSYS2 and standardize — mixed environments multiply support load. Assign the pre-class check gfortran --version (Exercise 2.1) so install failures surface before the session, not during it.

Connections

Back: Ch. 1 (the project domain chosen there becomes the program heat banner here). Forward: Ch. 3 (finally explains real(dp)/_dp), Ch. 8 (separate compilation — the payoff of the -c step), Ch. 13 (the rest of the debugging flags), Ch. 17 (the fixed-form source previewed in §2.5), Ch. 30 (the deeper optimization flags), Ch. 37 (reproducibility — seeded by "record your compile command"). Naming these now sets up the arc.