Skip to content
Part IA Michaelmas Term

Procedural Programming Overview

Functional vs procedural

Functional programmingProcedural programming
Expressions compute valuesCommands transform state
No side effectsState mutation is the norm
Recursion for repetitionLoops (while, for) for repetition
Immutable dataMutable variables, arrays, references

What procedural programs do

Procedural programs repeatedly transform a program state by executing commands or statements. A state change might be:

  • Updating a variable or array (local to the machine)
  • Sending data to the outside world (output)
  • Reading data from the environment (input, which also changes state by consuming data)

Building blocks

ConstructPurpose
AssignmentUpdate variables: x := E
Input/OutputCommunicate with environment
if / matchConditional execution
whileRepetition
Procedures (functions)Package commands as reusable abstractions

The need for subroutines (procedures) was evident from the earliest days of programming. They represent one of the first examples of abstraction in programming languages.

OCaml’s blend

OCaml makes no distinction between commands and expressions. Everything is an expression that returns a value. However:

  • Commands typically return () of type unit - the trivial value carrying no information.
  • The semicolon ; sequences expressions, discarding intermediate results.
  • OCaml programmers often use a functional style for internal computation and imperative features for I/O.

This dual nature means OCaml can teach procedural concepts while keeping the safety guarantees of a strongly typed functional language.