Programming
Cambridge AS & A Level Computer Science 9618 · Paper 2 · Unit 11
Paper 2 · Unit 11. Built-in functions, constructs, procedures, functions and parameters.
1. Variables, constants and built-in functions
Declare variables with a data type, and use constants for values that never change (easier to update, and they cannot be changed by mistake).
DECLARE Total : INTEGER
CONSTANT VAT = 0.2
Name ← INPUT
Len ← LENGTH(Name)
First ← LEFT(Name, 1)
Middle ← MID(Name, 2, 3)
Last ← RIGHT(Name, 2)
Up ← UCASE(Name)
N ← INT(7.8) // 7
R ← RAND(10) // real 0 to <10
S ← NUM_TO_STR(42)
X ← STR_TO_NUM("3.5")
C ← MOD(17, 5) // 2
D ← DIV(17, 5) // 3
2. Programming constructs
| Construct | Pseudocode |
|---|---|
| Sequence | Statements run in order |
| Selection | IF … THEN … ELSE … ENDIF, CASE OF … OTHERWISE … ENDCASE |
| Count-controlled loop | FOR … TO … STEP … NEXT |
| Pre-condition loop | WHILE … ENDWHILE (may run zero times) |
| Post-condition loop | REPEAT … UNTIL (runs at least once) |
Try it: name the programming construct
3. Procedures and functions
A procedure performs a task; a function returns a single value and is used in an expression. Subroutines make code modular, reusable and easier to test and maintain.
PROCEDURE Swap(BYREF A : INTEGER, BYREF B : INTEGER) DECLARE Temp : INTEGER Temp ← A A ← B B ← Temp ENDPROCEDURE FUNCTION Square(BYVAL N : INTEGER) RETURNS INTEGER RETURN N * N ENDFUNCTION CALL Swap(X, Y) OUTPUT Square(5)
| Passing | Effect |
|---|---|
| BYVAL | A copy of the value is passed; changes inside do not affect the original |
| BYREF | The address is passed; changes inside change the original variable |
Variables declared inside a subroutine are local; those declared in the main program are global. Prefer local variables to avoid unexpected side effects.
4. Examiner tips
- Function vs procedure: a function returns a value.
- BYREF is needed when the subroutine must change the caller’s variable.
- Choose the right loop and justify it (e.g. REPEAT for input validation).
- Use the exact built-in function names from the insert: MID, LENGTH, UCASE, MOD, DIV.
- Initialise totals and counters before loops.
5. Practice questions
Practice: answer and check instantly
Type a short answer, then press Check or Enter.
Show answer
The remainder of 17 ÷ 5.
Show answer
Integer division.
Show answer
5 characters.
Show answer
Characters 4–6.
Show answer
REPEAT … UNTIL.
Show answer
By reference.
Show answer
A function.
Show answer
INT truncates.
Exam-style questions
9. Explain the difference between passing a parameter by value and by reference. [2]
Show mark guide
By value passes a copy, so changes inside the subroutine do not affect the original [1] · by reference passes the address, so changes affect the original variable [1].
10. Write a function IsEven that takes an integer and returns TRUE if it is even. [4]
Show mark guide
FUNCTION IsEven(N : INTEGER) RETURNS BOOLEAN [1] · uses MOD(N, 2) = 0 [1] · returns TRUE/FALSE correctly [1] · ENDFUNCTION [1].
6. Topic test: predict your grade
Topic test: predict your grade
15 questions, about 10 minutes. Answer them all, then press Mark my test.
Frequently asked questions
What is the difference between BYVAL and BYREF?
BYVAL passes a copy of the value, so changes inside the subroutine do not affect the original. BYREF passes the address, so changes inside the subroutine change the original variable.
What is the difference between a WHILE loop and a REPEAT loop?
A WHILE loop checks its condition at the start and may run zero times. A REPEAT UNTIL loop checks at the end, so it always runs at least once.
What do MOD and DIV do?
MOD returns the remainder of a division and DIV returns the whole-number result. For example, MOD(17, 5) is 2 and DIV(17, 5) is 3.