SLIDE 1
CSZone.co.uk
Click to advance · Arrow keys also work
CAIE 9618 · Paper 2 · Topic 2.4.1

Programming
Concepts

OOP · Scope · Parameters (Value vs Reference) · Recursion

CSZone Cambridge International AS & A Level Computer Science 9618
Object-Oriented Programming

Classes, Objects & the 4 Pillars

OOP models programs around objects — instances of classes. A class is a blueprint; an object is a created instance with its own attribute values.
ENCAPSULATION
Bundling data (attributes) and methods together in a class. Private attributes accessed only through public methods (getter/setter). Hides internal implementation.
INHERITANCE
A subclass inherits attributes and methods from a parent class. Avoids code duplication. e.g. Dog INHERITS Animal.
POLYMORPHISM
The same method name behaves differently for different classes. e.g. makeSound() on Dog returns "Woof", on Cat returns "Meow". Method overriding.
ABSTRACTION
Hiding complex implementation details. Users interact with a simplified interface (public methods) without knowing how they work internally.
CAIE OOP Pseudocode

CLASS Definition and Instantiation

CLASS Animal
PRIVATE name : STRING
PRIVATE age : INTEGER
PUBLIC PROCEDURE NEW(n:STRING, a:INTEGER)
name <- n
age <- a
ENDPROCEDURE
PUBLIC FUNCTION getName() : STRING
RETURN name
ENDFUNCTION
ENDCLASS
INSTANTIATE AND USE
DECLARE myPet : Animal
myPet <- NEW Animal("Rex", 3)
OUTPUT myPet.getName() // "Rex"
INHERITANCE
CLASS Dog INHERITS Animal
PUBLIC PROCEDURE bark()
OUTPUT "Woof!"
ENDPROCEDURE
ENDCLASS
Variable Scope & Parameters

Local, Global, By Value, By Reference

SCOPE
Local variable — declared inside a procedure/function. Only accessible within that block. Created when procedure called, destroyed when it ends.

Global variable — declared at program level. Accessible anywhere. Discouraged — can cause unintended side effects.
PASS BY VALUE
A copy of the argument is passed. Changes inside the procedure do NOT affect the original variable. CAIE: PROCEDURE p(x : INTEGER)
PASS BY REFERENCE
The memory address is passed. Changes inside the procedure DO affect the original variable. CAIE: PROCEDURE p(BYREF x : INTEGER)
Recursion

A Function Calling Itself

A recursive function calls itself with a simpler version of the problem, until it reaches a base case (stopping condition). Requires: a base case and progress towards it.
FUNCTION factorial(n:INTEGER):INTEGER
IF n = 0 THEN // base case
RETURN 1
ELSE
RETURN n * factorial(n-1)
ENDIF
ENDFUNCTION

// Call: factorial(4)
// → 4 * factorial(3)
// → 4 * 3 * factorial(2)
// → 4 * 3 * 2 * factorial(1)
// → 4 * 3 * 2 * 1 * 1 = 24
MECHANICS
Each call pushes a stack frame (local vars, return address) to the call stack. Base case triggers return. Stack unwinds and results propagate back up.
RISKS
Stack overflow if base case never reached (infinite recursion). Higher memory usage than iteration (call stack grows). Each call has overhead.
Exam Practice

Cambridge-style questions

Question 1
Explain the difference between passing a parameter by value and by reference. State one situation where each would be appropriate.
4 marks
1+1
By value: a copy of the argument is passed — changes inside the procedure do not affect the original. Appropriate when the procedure should only read the value without modifying it (e.g. calculate and return a result).
1+1
By reference: the memory address of the argument is passed — changes inside the procedure modify the original variable. Appropriate when the procedure needs to update a variable in the calling program (e.g. swapping two values, sorting in place).
Common Mistakes

Don't lose easy marks

1
Saying that all parameters are passed by reference in CAIE pseudocode — by default, parameters are passed by value in CAIE pseudocode. You must explicitly write BYREF to pass by reference. Never assume reference passing.
2
Not including a base case in a recursive function — without a base case, the recursion never terminates, causing stack overflow. Always identify and state the base case explicitly. The mark scheme looks for this.
3
Confusing class and object — a class is the blueprint/template (e.g. Animal class). An object is a specific instance of that class with real values (e.g. myPet = Animal("Rex", 3)). You can have many objects from one class.
Topic Summary — 2.4.1

What You Need to Know

OOP CONCEPTS
Encapsulation: bundle data + methods
Inheritance: subclass inherits parent
Polymorphism: same method, different behaviour
Abstraction: hide implementation detail
PARAMETERS & SCOPE
By value: copy — no side effect
BYREF: address — modifies original
Local: within block · Global: everywhere
RECURSION
Must have base case + progress towards it
Uses call stack · Risk: stack overflow
Elegant for divide-and-conquer, tree traversal
CSZone

Next Video

2.4.2
Functions & Procedures
FUNCTION vs PROCEDURE · Built-in Functions · Libraries
Head to CSZone.co.uk for the complete worksheet, quiz, and interactive tools