CSC 280 · Introduction to Computer Programming
Our notes
1 min readWhy FORTRAN
FORTRAN (FORmula TRANslation, IBM, 1957) was the first widely used high-level language and is still used for heavy science and engineering computing (weather, physics simulations). UNIPORT's handbook teaches CSC 280 in FORTRAN. Modern free-form FORTRAN (90/95) is shown here; old FORTRAN 77 used fixed columns (statements from column 7, C in column 1 for comments).
Program layout
PROGRAM average
IMPLICIT NONE
REAL :: a, b, c, avg
PRINT *, 'Type three numbers'
READ *, a, b, c
avg = (a + b + c) / 3.0
PRINT *, 'Average = ', avg
END PROGRAM average
- IMPLICIT NONE turns off FORTRAN's old rule that names starting with I to N are integers, so every variable must be declared (catches spelling mistakes).
- ! starts a comment. FORTRAN is not case-sensitive.
Data types and declarations
INTEGER :: n, count; REAL :: x; DOUBLE PRECISION :: big; CHARACTER(LEN=20) :: name; LOGICAL :: done; COMPLEX :: z.
Constants: REAL, PARAMETER :: pi = 3.14159
Operators
+ - * / and power (2∗∗3 = 8, written with two stars). Integer division drops the fraction: 7/2 = 3, but 7.0/2 = 3.5. MOD(7, 2) = 1.
Relational: == /= < > <= >= (old form .EQ. .NE. .LT. .GT. .LE. .GE.). Logical: .AND. .OR. .NOT.
Decisions
IF (score >= 40) THEN
PRINT *, 'Pass'
ELSE
PRINT *, 'Fail'
END IF
Chains use ELSE IF (…) THEN. Many fixed choices:
SELECT CASE (day)
CASE (1:5)
PRINT *, 'Weekday'
CASE DEFAULT
PRINT *, 'Weekend'
END SELECT
DO loops
DO i = 1, 10, 2
PRINT *, i
END DO
prints 1, 3, 5, 7, 9 (start, end, step; the end value is included, unlike Python's range).
Condition loop: DO WHILE (n <= 100) … END DO. EXIT leaves a loop; CYCLE skips to the next pass.
Sum of 1 to N: total = 0; DO i = 1, n; total = total + i; END DO.
Arrays
REAL :: scores(5) declares scores(1) to scores(5) (index from 1). Two-dimensional: REAL :: m(3, 3). Whole-array operations work: total = SUM(scores), big = MAXVAL(scores).
DO i = 1, 5
READ *, scores(i)
END DO
Functions and subroutines
REAL FUNCTION area(r)
REAL, INTENT(IN) :: r
area = 3.14159 * r * r
END FUNCTION area
SUBROUTINE swap(a, b)
REAL, INTENT(INOUT) :: a, b
REAL :: t
t = a; a = b; b = t
END SUBROUTINE swap
Call them with y = area(2.0) and CALL swap(x, y). Arguments are passed by reference, so swap really changes x and y.
Formatted output
PRINT '(A, F8.2)', 'Average = ', avg prints the number in 8 places with 2 decimals. Codes: I for integers (I5), F for reals (F8.2), E for exponent form, A for text, X for spaces.
Exams usually ask: write a complete FORTRAN program (average, largest, sum of a series, quadratic roots); declarations and IMPLICIT NONE; DO loops and their counts; integer division; arrays and a subroutine; format codes.
Common mistake: 1/2 in FORTRAN gives 0 (integer division). Write 1.0/2.0 for 0.5.
Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.
Read
-
Best pick Hello world — Fortran Programming Language
(opens fortran-lang.org in a new tab)
-
Variables — Fortran Programming Language
(opens fortran-lang.org in a new tab)
-
Operators and flow control — Fortran Programming Language
(opens fortran-lang.org in a new tab)
-
Arrays and strings — Fortran Programming Language
(opens fortran-lang.org in a new tab)
-
Organising code structure — Fortran Programming Language
(opens fortran-lang.org in a new tab)
-
Fortran
(opens en.wikipedia.org in a new tab)
Wikipedia, Fortran, CC BY-SA 4.0 · licence
Watch
We link to free material and never copy it. Every link was opened by our team before it went up; video data is a rough guide at 360p.