UNIHIVE

Study Hub UNIPORT ENG 213.1

ENG 213.1 · Computer Programming for Engineers

Fortran program structure, variables and data types

The parts of a Fortran program, naming rules, IMPLICIT NONE, the intrinsic types and constants.

Our notes

2 min read

Structure 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:

  1. program name line.
  2. implicit none: every variable must be declared.
  3. Declarations: type and name of each variable.
  4. Executable statements: calculations, input, output.
  5. 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

TypeHoldsExample declaration
integerwhole numbersinteger :: n, count
realsingle precision real numbers (about 7 digits)real :: velocity
double precision (or real(kind=8))about 15-16 digitsreal(kind=8) :: stress
complexa + bicomplex :: z
logical.true. or .false.logical :: converged
charactertextcharacter(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

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.

All ENG 213.1 topics

Report

Sign in to report this. It is free.

Is this your work (a lecturer, author or publisher)? Ask us to take it down.