ENG 213.1 · Computer Programming for Engineers
Our notes
2 min readStructure of a program
program circle
implicit none
real :: r, area
real, parameter :: pi = 3.14159265
r = 2.5
area = pi * r**2
print *, 'Area =', area
end program circle
Output: Area = 19.6349545 (the last digits vary with the compiler).
Parts:
- program name line.
- implicit none: every variable must be declared.
- Declarations: type and name of each variable.
- Executable statements: calculations, input, output.
- end program line.
Names
Start with a letter; then letters, digits and underscores; up to 63 characters in modern Fortran. Fortran does not distinguish capitals: Area, AREA and area are the same name. Use meaningful names: flow_rate, not x7.
Intrinsic data types
| Type | Holds | Example declaration |
|---|---|---|
| integer | whole numbers | integer :: n, count |
| real | single precision real numbers (about 7 digits) | real :: velocity |
| double precision (or real(kind=8)) | about 15-16 digits | real(kind=8) :: stress |
| complex | a + bi | complex :: z |
| logical | .true. or .false. | logical :: converged |
| character | text | character(len=20) :: name |
Why IMPLICIT NONE
Without it, old Fortran treats undeclared names beginning with I to N as integers and others as reals. A typing mistake (veloctiy for velocity) then creates a new variable silently, and the answer is wrong with no error. IMPLICIT NONE makes the compiler report it.
Constants
- Literal: 42, 3.0, 2.5e-3 (= 0.0025), 'steel', .true.
- Named constants with parameter, which cannot be changed:
real, parameter :: g = 9.81, rho_water = 1000.0
Comments
Anything after ! on a line is a comment. Use comments to explain units and the purpose of each block:
real :: q ! flow rate in m^3/s
Continuation
End a long line with & to continue on the next line.
Exams usually ask
- Write the structure of a simple Fortran program and explain each part.
- State the Fortran data types with examples.
- Explain why IMPLICIT NONE is good practice.
Common mistake
Declaring a variable that holds a decimal value as integer: integer :: v then v = 2.7 stores 2, silently cutting off the fraction.
Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.
Read
-
Fortran
(opens en.wikipedia.org in a new tab)
Wikipedia, Fortran, CC BY-SA 4.0 · licence
-
Data type
(opens en.wikipedia.org in a new tab)
Wikipedia, Data type, CC BY-SA 4.0 · licence
Watch
-
Best pick INTRODUCTION TO SYSTEM ANALYSIS USING FORTRAN 2
9 min · ≈ 41 MB · about ₦23
(opens youtube.com in a new tab)
-
Fortran Variables Explained | Modern Fortran Course – Lesson 1.1
20 min · ≈ 90 MB · about ₦51
(opens youtube.com in a new tab)
-
Data Types & Variable Assignments | FORTRAN Tutorial - 4
12 min · ≈ 54 MB · about ₦30
(opens youtube.com in a new tab)
-
Advanced Fortran Programming : 021 : Derived Data Types
24 min · ≈ 108 MB · about ₦61
(opens youtube.com in a new tab)
-
FORTRAN Lecture 2 | Variables, Data Types & Input Output 🔥
6 min · ≈ 27 MB · about ₦15
(opens youtube.com in a new tab)
-
Fortran Tutorial
1 h 13 min · ≈ 329 MB · about ₦185
(opens youtube.com in a new tab)
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.