Fortran Tutorial
Learn Fortran step by step: 39 lessons in 9 chapters with examples, quizzes, diagrams and projects.
Start learning →
of lessons completed
Getting Started
- What Is Fortran? Meet Fortran, the oldest high-level language still used for weather models, physics and supercomputers. Learn its history, standards, strengths and what you will build.
- Installing a Fortran Compiler Install gfortran on Windows, macOS or Linux, compile and run a Fortran program from the command line, and learn the most useful compiler flags for beginners.
- Your First Fortran Program Write and understand your first Fortran program: program, implicit none, declarations, assignments, print and write, comments, and a simple area calculator.
- Program Structure and Source Form Understand how a Fortran program is organized: specification and execution parts, program units, naming rules, free-form vs fixed-form source, and good style.
Language Basics
- Variables and Data Types Learn Fortran's five intrinsic types integer, real, complex, logical and character, how to declare variables and constants with parameter, and how to initialize them.
- Kinds and Precision Control the size and precision of Fortran numbers with kind parameters: selected_real_kind, selected_int_kind, iso_fortran_env, double precision and literal suffixes.
- Operators and Expressions Learn Fortran's arithmetic, relational, logical and character operators, precedence rules, integer division, exponentiation and mixed-type expressions with examples.
- Characters and Strings Work with text in Fortran: fixed-length character variables, substrings, concatenation, trim, len_trim, index, adjustl, comparisons and allocatable deferred-length strings.
- Reading and Writing Basics Learn Fortran print, write and read statements, list-directed output, format descriptors I, F, ES, A and X, line control with slash, advance=no and reading numbers from the keyboard.
Control Flow
- The if Statement and Logical Expressions Make decisions in Fortran with the single-line if, if-then-else blocks, else if chains, nested ifs, logical operators and safe comparisons of real numbers.
- The select case Statement Use Fortran's select case to choose between many alternatives: single values, lists, ranges, case default, character selectors, and why there is no fall-through.
- do Loops Repeat work in Fortran with counted do loops, steps and countdowns, do while, endless do with exit, nested loops, the trip count rule and implied do loops for printing.
- exit, cycle and Named Constructs Control loops precisely in Fortran: exit and cycle, naming constructs to leave outer loops, the block construct, stop and error stop, and an introduction to do concurrent.
Arrays and Numerics
- Arrays: The Basics Learn Fortran arrays from scratch: declaring 1D and 2D arrays, custom lower bounds, array constructors, implied do loops, column-major storage, size, shape and reshape.
- Array Sections and Whole-Array Operations Slice Fortran arrays with start:end:stride sections, operate on whole arrays without loops, use elemental functions, row and column sections of matrices and vector subscripts.
- Array Intrinsic Functions Master Fortran's array intrinsic functions: sum, product, maxval, minloc, count, any, all, dot_product, matmul, transpose, pack, spread, cshift and the dim and mask arguments.
- where and Masking Apply operations only to selected array elements with where, elsewhere, masked assignment, merge, pack and unpack, and learn why forall gave way to do concurrent.
- Allocatable Arrays Create arrays whose size is chosen at run time with Fortran allocatable arrays: allocate, deallocate, allocated, stat, automatic reallocation on assignment, source, mold and move_alloc.
- Numeric Intrinsics and Floating-Point Behavior Use Fortran's math intrinsics sqrt, exp, log, sin, atan2, hypot, mod, nint and complex functions, and understand rounding, NaN, Infinity, epsilon and ieee_arithmetic.
Procedures and Modules
- Subroutines and Functions in Fortran Learn how Fortran splits work into functions and subroutines: the result clause, call statements, internal procedures, and how arguments are passed by reference.
- Argument Intent, Optional and Keyword Arguments Use intent(in), intent(out) and intent(inout), optional arguments with present(), and keyword calls to write safe, self-documenting Fortran procedures.
- Fortran Modules and the use Statement Organize Fortran code with modules: share constants, variables and procedures, control visibility with private and public, rename with use, and compile in order.
- Interfaces, Generic Procedures and Operator Overloading Use interface blocks to write generic procedures that accept several types, overload operators, pass procedures as arguments, and describe external code.
- Recursion, Pure and Elemental Procedures Write recursive Fortran procedures with the recursive keyword and result clause, and use pure and elemental to make side-effect-free, array-friendly functions.
Derived Types and Pointers
- Derived Types in Fortran Group related data with Fortran derived types: define types, build constructors, access components with %, use default values, nest types and keep arrays of structures.
- Fortran Pointers, Targets and Allocatables Understand Fortran pointers and the target attribute, pointer assignment with =>, associated(), allocatable arrays versus pointers, and build a linked list safely.
- Type-Bound Procedures and Object-Oriented Fortran Attach procedures to derived types with the contains section, call them with the object%method syntax, use passed-object dummies, constructors and final procedures.
- Inheritance, Polymorphism and Abstract Types Extend derived types with extends, override type-bound procedures, write abstract types with deferred procedures, and use class(*), select type and polymorphic arrays.
File I/O and Formatting
- Formatted Output and Edit Descriptors in Fortran Control Fortran output with format specifiers: I, F, E, ES, A, L, X and slash descriptors, repeat counts, reversion, internal writes and tidy tables of numbers.
- Reading and Writing Files in Fortran Work with files in Fortran: open and close units, newunit, status and action, read until end of file with iostat, check errors, rewind, inquire and use scratch files.
- Namelists, Stream Access and Unformatted Files Read configuration with Fortran namelists, write fast unformatted binary files, use direct and stream access, and exchange data safely between programs.
Parallel and Scientific Computing
- do concurrent, Array Operations and Vectorization Write fast, parallel-friendly Fortran: whole-array syntax, where and forall masks, do concurrent loops with locality, memory layout, and vectorization.
- OpenMP Shared-Memory Parallelism in Fortran Parallelize Fortran loops with OpenMP directives: parallel do, private and shared, reduction, critical, atomic, schedule, collapse, and data race pitfalls.
- Coarrays: Parallel Programming Built into Fortran Learn Fortran coarrays: images, this_image and num_images, the [ ] cosubscript, sync all, collective subroutines co_sum, critical sections, and compiling with OpenCoarrays.
- Fortran and C Interoperability with iso_c_binding Call C from Fortran and Fortran from C with iso_c_binding: bind(c), c_int and c_double kinds, value arguments, C strings, arrays, structs and callbacks.
Projects and Practice
- Project: A Statistics Module Build a reusable Fortran statistics module with mean, variance, standard deviation, median, correlation and histogram, then test it against known values.
- Project: 1D Heat Diffusion Simulation Simulate heat flow along a rod in Fortran with an explicit finite-difference scheme: array slices, stability limit, boundary conditions, output files and plotting.
- Project: A Linear System Solver Write a linear equation solver in Fortran: Gaussian elimination with partial pivoting, matmul checks, residuals, singular matrices, and comparison with LAPACK dgesv.
- Fortran Practice Problems with Solutions Ten Fortran exercises on procedures, modules, derived types, file I/O and numerical methods, each with hints, a full solution and a runnable Python check.