Appearance
D1 · Procedural Programming
Spec reference: Section D - Programming Languages
Key idea: Programs are sequences of instructions organised into procedures/functions that run step-by-step.
Structure of a procedural program
A procedural program typically consists of:
- Variable declarations - define the data the program will use.
- Main program body - the entry point, calling procedures in order.
- Procedures and functions - named blocks of reusable code.
python
# 1. Variables / constants
MAX_SCORE = 100
student_name = ""
score = 0
# 2. Procedures and functions
def get_input():
name = input("Enter student name: ")
score = int(input("Enter score: "))
return name, score
def calculate_grade(score):
if score >= 70:
return "Distinction"
elif score >= 55:
return "Merit"
elif score >= 40:
return "Pass"
else:
return "Fail"
def display_result(name, grade):
print(f"{name}: {grade}")
# 3. Main program - calls procedures in order
student_name, score = get_input()
grade = calculate_grade(score)
display_result(student_name, grade)Key features of procedural programming
Top-down design
Programs are designed from the general (the overall problem) down to the specific (the individual procedures). The main program describes what to do; procedures describe how.
MAIN PROGRAM
├── get_student_data()
│ ├── validate_name()
│ └── validate_score()
├── process_results()
│ ├── calculate_grade()
│ └── calculate_rank()
└── produce_report()
├── format_output()
└── write_to_file()Sequence
Instructions execute in a defined order, one after another. The order matters.
Modularity
Code is split into separate procedures - each one doing one specific job. This makes programs:
- Easier to read and understand
- Easier to test individually
- Easier to reuse in other programs
- Easier to maintain - change one procedure without breaking others
Local vs global scope
Each procedure has its own local variables that don't interfere with other procedures:
python
def calculate_vat(price):
rate = 0.20 # local - only exists here
return price * rate
def calculate_discount(price):
rate = 0.10 # different local variable - no conflict
return price * rateControl structures in procedural programming
Procedural programs use all three control structures:
python
# Sequence
x = 5
y = 10
total = x + y
# Selection
if total > 10:
print("Large")
else:
print("Small")
# Iteration
for i in range(1, 6):
print(i)Advantages and disadvantages
| Advantages | Disadvantages |
|---|---|
| Simple, easy to understand | Can become hard to manage in very large programs |
| Code is reusable through procedures | Data is often shared globally - can cause unintended changes |
| Easy to trace and debug | Not well suited to modelling real-world objects |
| Good performance - straightforward execution | Doesn't scale well without careful design |
Example: Number guessing game (procedural style)
python
import random
def generate_number():
return random.randint(1, 100)
def get_guess():
while True:
try:
guess = int(input("Guess (1-100): "))
if 1 <= guess <= 100:
return guess
except ValueError:
pass
print("Invalid - enter a number between 1 and 100.")
def check_guess(guess, target):
if guess < target:
return "Too low"
elif guess > target:
return "Too high"
else:
return "Correct"
def play_game():
target = generate_number()
attempts = 0
while True:
guess = get_guess()
attempts += 1
result = check_guess(guess, target)
print(result)
if result == "Correct":
print(f"Well done! {attempts} attempts.")
break
play_game()Summary
| Term | Meaning |
|---|---|
| Procedural programming | Paradigm organising code as sequences of procedures |
| Top-down design | Breaking the overall program into smaller sub-tasks |
| Procedure | Named block of code performing a single task |
| Modularity | Splitting a program into independent, reusable units |
| Local variable | Variable only accessible within the procedure it is declared in |
Test Yourself
Question 1 of 5
What is the core principle of procedural programming?