Functions and Debugging
How a beginner program is built, tested and repaired - in the order a marker looks for
Computer Science | Undergraduate (Level 4) | December 2026
WHAT IS INSIDE
1. Variables, types and expressions
2. Control flow: selection and iteration
3. Functions, parameters and decomposition
4. Testing, debugging and program quality
Exam question patterns
Glossary of exam-ready definitions
1. Variables, types and expressions
A variable is a named location holding a value of a particular type; what the type allows determines what the
program can do without crashing. Primitive types - integer, floating point, boolean, character - are stored
directly, while reference types such as strings and objects hold the location of data. Static typing checks
types before the program runs and catches mistakes early; dynamic typing checks at runtime and trades that
safety for flexibility, which is why the same one-line error becomes a compile error in one language and a
crash in another.
Declaring, initialising and assigning are three separate acts and exam questions test them separately: an
uninitialised variable holds whatever the language's default rule gives it (not necessarily zero), a reassignment
of an incompatible type is a type error in a static language and a silent conversion or runtime failure in a
dynamic one. Expressions combine literals, variables and operators; operator precedence and integer
division are the two sources of most wrong answers, since 7/2 is 3 in integer arithmetic and 3.5 in floating
point.
• Scope - a variable exists only inside the block in which it is declared; using it outside is the most common
beginner error.
• Immutability - a constant cannot be reassigned; declare it so when the value must not change.
• Casting - widening conversions are safe, narrowing ones truncate or throw; state which in your answer.
• Naming - a descriptive identifier is marked in a trace question, because the reader must follow the state.
2. Control flow: selection and iteration
Selection chooses a path: if/else chains test conditions in order and stop at the first true branch, while a
switch or match compares one value against constant cases. The condition must be a boolean expression, and
the three classic bugs are an assignment written where a comparison was meant, an off-by-one boundary, and
a missing else that leaves a path unhandled. Short-circuit evaluation means the second operand of and/or is
not evaluated when the result is already decided - a detail that exam questions use to explain why a null check
must come first.
Iteration repeats work: a for loop is appropriate when the number of repetitions is known, a while loop when
it depends on a condition, and a do-while when the body must run at least once. Nested loops multiply their
counts, which is the source of the complexity result most students state without justification. break and
continue change the loop's control, and an infinite loop is diagnosed by showing which variable the loop
depends on never changes.
Programming Fundamentals: Variables, Control Flow, Functions Page 1 of 3