← Back to subjects
0
AQA A-level Computer Science (7517) · Fundamentals of programming
Mini-Lesson

Fundamentals of programming

This mini-lesson covers the whole of AQA 4.1 — Fundamentals of programming. It is the bedrock of Paper 1: everything you write in the exam's Skeleton Program leans on it.

  • Data types and how they are stored
  • Constructs, operators, DIV and MOD
  • Subroutines, parameters (by value / by reference) and scope
  • Recursion — base case, general case, and the call stack
  • Object-oriented programming — encapsulation, inheritance, polymorphism
  • Exception handling

Work through each screen, answer the questions as you go (some are recall, some are calculations you must actually work out) and collect ⭐ stars. Press Start when you're ready.

4.1 · Data types

Data types

A data type tells the compiler two things: what values a variable may hold, and what operations are legal on it. AQA expects you to know these:

TypeHoldsTypical size
integerwhole numbers, positive or negative2 or 4 bytes
real / floatnumbers with a fractional part4 or 8 bytes
BooleanTrue or False only1 bit (usually padded to 1 byte)
charactera single symbol, e.g. 'A'1 byte (ASCII) / 1–4 (Unicode)
stringa sequence of charactersvaries
date/timea point in timevaries
pointer / referencea memory address4 or 8 bytes
recordfields of mixed type grouped togethersum of its fields

Why it matters: choosing integer over real where you can is not fussiness — integers are exact, while reals are stored in floating point and carry rounding error. Money is best held as an integer number of pence.

4.1 · Constructs & operators

Constructs, DIV and MOD

Three constructs are enough to express any computable algorithm (this is the structure theorem): sequence, selection and iteration.

  • Sequence — statements executed one after another.
  • Selection — IF / ELSE IF / ELSE, or CASE/SWITCH.
  • Iterationdefinite (FOR: a known number of repeats) or indefinite (WHILE / REPEAT: repeats until a condition changes).

Two arithmetic operators that catch people out:

a DIV b = integer quotienta MOD b = the remainder left over
17 DIV 5 = 3 (5 goes into 17 three whole times) 17 MOD 5 = 2 (17 − 15 = 2 left over) 17 / 5 = 3.4 (real division — a different type!)

Classic use: MOD 2 = 0 tests for even. MOD also wraps a value round a circular buffer, and DIV/MOD together split a total of seconds into minutes and seconds.

Calculate

Your turn — DIV and MOD

1Using AQA pseudo-code rules, evaluate (47 DIV 5) * 10 + (47 MOD 5).
Hint: 47 DIV 5 is the whole number of times 5 fits into 47; 47 MOD 5 is what is left over.
Quick check

Integer vs real division

?A program stores total as an integer. Which single line is guaranteed to leave an integer result in every language AQA supports?
4.1 · Subroutines

Subroutines and parameters

A subroutine is a named block of code that can be called from elsewhere. AQA distinguishes:

  • a procedure — performs an action, returns no value;
  • a function — returns exactly one value to the point of call.

Values passed in are parameters (in the definition) and arguments (at the call). There are two ways to pass them:

By valueBy reference
A copy of the value is passed.The memory address of the variable is passed.
Changes inside the subroutine are lost on return.Changes inside the subroutine persist in the caller.
Safer; costs time and memory for large data.Efficient for large arrays; risks unintended side effects.
PROCEDURE bump(BYVAL x, BYREF y) x ← x + 1 y ← y + 1 ENDPROCEDURE a ← 5 : b ← 5 bump(a, b) OUTPUT a, b // prints 5 6

Watch out: in Python and Java, objects and lists are passed by object reference — mutating the list inside a function does change the caller's list, even though reassigning the name does not.

Quick check

Parameter passing

?A subroutine is passed a 100 000-element array. The subroutine sorts the array in place, and the caller expects to see the sorted result. Which passing mechanism is required, and why?
4.1 · Scope

Local and global scope

Scope is the region of a program in which an identifier is visible.

  • A local variable is declared inside a subroutine. It is created when the subroutine is called and destroyed when it returns. It cannot be seen outside.
  • A global variable is declared outside all subroutines and is visible everywhere for the whole run of the program.

Locals are preferred because they:

  • prevent accidental side effects from unrelated code;
  • let the same name be reused safely in different subroutines;
  • free their memory on return, and make subroutines self-contained and reusable.
count ← 0 // global PROCEDURE tally() count ← count + 1 // touches the global temp ← count * 2 // local — gone at ENDPROCEDURE ENDPROCEDURE OUTPUT temp // ERROR: temp is out of scope

Shadowing: if a local has the same name as a global, the local wins inside that subroutine — the global is hidden, not overwritten.

Quick check

Scope

?Why is heavy use of global variables considered poor practice in a large program?
4.1 · Recursion

Recursion

A subroutine is recursive if it calls itself. Every valid recursive routine needs:

  • a base case (stopping condition) that returns without recursing;
  • a general case that calls itself with arguments moving towards the base case;
  • a guarantee the base case is always reached — otherwise you get infinite recursion and a stack overflow.
FUNCTION fact(n) IF n = 0 THEN // base case RETURN 1 ELSE RETURN n * fact(n-1) // general case ENDIF ENDFUNCTION

Each call pushes a stack frame onto the call stack holding the return address, the parameters and the locals. The frames unwind in reverse (last in, first out) as each call returns.

Trade-off: recursion is elegant for problems with a naturally recursive structure (tree traversal, quicksort, merge sort, Towers of Hanoi) but uses more memory than iteration and is slower because of the call overhead. Any recursive routine can be rewritten iteratively, using an explicit stack if necessary.

Calculate

Your turn — trace the recursion

2Trace this routine and give the value of mystery(6).
FUNCTION mystery(n) IF n = 0 THEN RETURN 1 ELSE RETURN n * mystery(n - 2) ENDIF ENDFUNCTION
Hint: Unwind it: mystery(6) = 6 × mystery(4), mystery(4) = 4 × mystery(2), mystery(2) = 2 × mystery(0), and mystery(0) = 1.
Calculate

Your turn — counting the calls

3For the naive Fibonacci function f(n) = f(n−1) + f(n−2) with base cases f(1) = f(2) = 1, how many times in total is f invoked when you evaluate f(5)? (Count the first call f(5) itself.)
calls
Hint: Draw the call tree. f(5) calls f(4) and f(3); f(4) calls f(3) and f(2); each f(3) calls f(2) and f(1). Count every node.
4.1 · OOP

Object-oriented programming — classes and encapsulation

A class is a blueprint: it defines the attributes (data) and methods (behaviour) that its instances will have. An object is an instance of a class, created by instantiation — the constructor runs and sets the initial state.

Encapsulation (information hiding) means attributes are declared private and can only be read or changed through public methods (getters and setters).

CLASS Account PRIVATE balance : Integer PUBLIC PROCEDURE New(opening) balance ← opening ENDPROCEDURE PUBLIC FUNCTION GetBalance() RETURN balance ENDFUNCTION PUBLIC PROCEDURE Deposit(amount) IF amount > 0 THEN balance ← balance + amount ENDPROCEDURE ENDCLASS

Because balance is private, no outside code can set it to a negative number: the class enforces its own invariants. The internal representation can also be changed later without breaking any code that uses the class.

4.1 · OOP

Inheritance, polymorphism, abstraction

Inheritance: a subclass derives from a superclass, gaining its attributes and methods and adding or changing its own. It models an is-a relationship — a Savings account is a kind of Account.

Overriding: the subclass supplies its own version of an inherited method with the same signature.

Polymorphism: one interface, many forms. The same call on different objects runs different code, chosen at run time by the object's actual class.

CLASS Shape PUBLIC FUNCTION Area() // generic ENDCLASS CLASS Circle INHERITS Shape PUBLIC FUNCTION Area() // overrides RETURN 3.14159 * r * r ENDCLASS FOR EACH s IN shapes OUTPUT s.Area() // polymorphic: the right Area() runs ENDFOR

Composition (has-a) is the alternative to inheritance: a Car has an Engine. The maxim "favour composition over inheritance" exists because deep inheritance hierarchies are brittle — a change high up breaks everything below.

Abstraction in OOP: the public interface tells you what an object does; the private implementation hides how. You can drive a car without knowing how the engine works.

Quick check

Polymorphism

?A list holds Circle, Square and Triangle objects, all subclasses of Shape. The program loops through the list calling s.Area() on each. Which OOP feature makes the correct version of Area() run for each object?
Quick check

Encapsulation

?A Temperature class stores celsius as a private attribute with a public SetCelsius() method that rejects values below −273.15. What does the private declaration achieve?
4.1 · Exceptions

Exception handling

An exception is a run-time error — a fault that only appears when the program actually runs (not a syntax error, which the translator catches first).

  • Dividing by zero
  • Converting "seven" to an integer
  • Indexing past the end of an array
  • Opening a file that does not exist

A try / except (catch) block lets the program recover instead of crashing:

TRY age ← CAST_TO_INT(userInput) EXCEPT ValueError OUTPUT "Please type a whole number" age ← 0 FINALLY closeFile() // always runs, error or not ENDTRY

Exam point: exception handling makes a program robust — it copes with invalid input and unexpected conditions gracefully rather than terminating. Do not use a bare catch-everything block that silently swallows errors: a bug you never see is worse than one that crashes.

Quick check

Exceptions

?Which of these is a run-time error that exception handling is designed to deal with?
Sort it

Sort the terms

Tap a term, then tap the group it belongs to.

🔢 Data type

🔁 Programming construct

🧱 OOP concept

Match it

Match the definition to the term

Tap an item on the left, then its partner on the right.

Definition
Term
Recap

The big ideas to know

Data types: integer · real · Boolean · character · string · date/time · pointer/reference · record

Operators: DIV = integer quotient, MOD = remainder, / = real division

Subroutines: procedure (no return) vs function (returns one value); by value = copy, by reference = address

Scope: local = created on call, destroyed on return; global = visible everywhere (avoid)

Recursion: base case + general case + progress towards the base case; each call adds a stack frame

OOP: class = blueprint, object = instance; encapsulation · inheritance (is-a) · polymorphism (overriding) · composition (has-a)

Exceptions: try / except / finally handles run-time errors so the program stays robust

That is the whole of AQA 4.1. Press Finish to see your score.

🏆

Mini-lesson complete!

⭐⭐⭐

You've worked through Fundamentals of programming for AQA A-level Computer Science (7517). 🎉

Your stars: 0 / 0

Next: test yourself in the Evaluate stage Confidence Quiz, then lock it in with Verify.

📣 Smashed it? Share your score

Challenge a mate to beat your stars, or show a parent how you got on.

→ Back to all subjects