SLIDE 1 / 11
CSZone.co.uk
OCR H446 · Component 2 · 2.2.1

Programming
Techniques

OCR A Level Computer Science · cszone.co.uk
H446 SpecA Level
Learning Objectives

By the end of this topic you will be able to:

Use and explain programming constructs: sequence, selection, iteration
Write recursive subroutines and trace their execution
Explain object-oriented programming: classes, objects, inheritance, polymorphism, encapsulation
Describe procedural and functional programming paradigms
Constructs

Programming Constructs

Sequence
Instructions execute one after another in order. The fundamental building block of all programs.
Iteration
for (count-controlled) and while/do…until (condition-controlled). Used to repeat a block. In A Level exams, know when to use each type.
Selection
if…elif…else and switch/case statements. Program chooses which branch to execute based on a condition. Can nest multiple levels deep.
Subroutines
Functions (return a value) and procedures (do not return). Parameters passed by value or by reference. Each has its own local scope.
Recursion

Recursion in Programming

A recursive subroutine calls itself. Every recursive solution must have: (1) a base case — the condition under which it stops recursing; (2) a recursive case — the call to itself with a modified parameter moving toward the base case.
Example: Factorial
def fact(n):
  if n == 1: return 1
  return n * fact(n-1)

# fact(4) = 4*fact(3) = 4*3*fact(2)
# = 4*3*2*fact(1) = 4*3*2*1 = 24
Call Stack
Each recursive call adds a stack frame to the call stack. The base case resolves, and frames unwind. Stack overflow occurs if base case is never reached — too many frames.
OOP: Classes & Objects

Object-Oriented Programming

A class is a template/blueprint defining attributes (data) and methods (behaviour). An object is an instance of a class — a specific realisation with its own attribute values.
Encapsulation: combining data (attributes) and methods in one unit (the class), and hiding internal implementation details. Attributes are typically private; access is through public getter/setter methods.
Constructor: the special method (__init__ in Python) called when an object is instantiated. It initialises the object's attributes to their starting values.
Encapsulation enforces the principle of information hiding — the object's internal state is protected from direct external manipulation, reducing bugs caused by unintended modification.
OOP: Inheritance & Polymorphism

Inheritance and Polymorphism

Inheritance: a child class inherits attributes and methods from a parent (base) class. The child can override inherited methods and add its own. Promotes code reuse and a natural class hierarchy. E.g. Dog inherits from Animal.
Polymorphism: objects of different classes can be treated as objects of a common superclass, and the correct method is called depending on the actual type of the object at runtime. E.g. makeSound() behaves differently for Dog and Cat even when called via an Animal reference.
Abstract class / interface: defines a set of methods that subclasses must implement. Cannot be instantiated directly. Ensures all subclasses share a common interface even if the implementation differs.
Programming Paradigms

Procedural vs Functional Programming

Procedural (Imperative)
Program is a sequence of instructions that change program state. Uses subroutines (procedures/functions) that may have side effects. Examples: Python, Pascal. Most common paradigm learned first at A Level.
Functional
Computation as evaluation of mathematical functions. Functions have no side effects (pure functions). Use recursion instead of iteration. Examples: Haskell. Functions can be treated as values (first-class/higher-order functions), passed as arguments.
OCR H446 also expects knowledge of declarative programming (e.g. SQL, Prolog) — describing what result is required rather than how to compute it. The programmer states facts and rules; the runtime determines how to compute the answer.
Exam Practice
OCR H446 Style · 5 marks
Describe three features of the object-oriented programming paradigm and explain one advantage of using OOP over procedural programming for a large software project.
[5 marks]
1
Encapsulation: data (attributes) and behaviour (methods) are bundled within a class. Attributes are private; access is through public methods, protecting data integrity.
1
Inheritance: child classes inherit attributes and methods from parent classes, enabling code reuse and a logical class hierarchy without duplication.
1
Polymorphism: objects of different classes respond to the same method call in their own way, allowing general code that works with objects of different types.
2
Advantage: OOP allows the large system to be broken into independent classes that can be developed and tested separately by different team members. Encapsulation prevents unintended interaction between modules, reducing bugs — making maintenance and extension much easier in large projects.
Common Mistakes

Don't Lose Marks

!
Saying inheritance means "a child class IS a parent class" without qualification — always state that the child class inherits the attributes and methods of the parent class and can override them. "Inherits its code" is too vague; "inherits attributes and methods" is what OCR mark schemes reward.
!
Confusing polymorphism with overloading — polymorphism in OCR H446 context is about the same method name behaving differently depending on the object's actual type (runtime). Students sometimes describe operator overloading which is a different concept.
!
Missing the base case when describing recursion — never describe a recursive function without explicitly stating the base case (the condition that stops recursion). Without a base case, a function recurses infinitely and causes a stack overflow.
Parameter Passing

Passing by Value vs Reference

Pass by Value
A copy of the argument is passed to the subroutine. Changes to the parameter inside the subroutine do not affect the original variable. Safe — prevents unintended modification of calling code's variables. Used in Python for immutable types (int, str).
Pass by Reference
A reference (address) to the original variable is passed. Changes inside the subroutine affect the original. Necessary when a subroutine must modify its arguments — e.g. swapping two values. Used for mutable types in Python (lists, dicts).
OCR H446 pseudo-code uses byVal and byRef keywords. Know that passing by reference is more memory efficient for large data structures (no copy made) but risks unintended side effects if the subroutine modifies the data unexpectedly.
2.2.1a Complete
Well done! ✓
Programming Techniques
Return to lesson to continue