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

ConstructPseudocode
SequenceStatements run in order
SelectionIF … THEN … ELSE … ENDIF, CASE OF … OTHERWISE … ENDCASE
Count-controlled loopFOR … TO … STEP … NEXT
Pre-condition loopWHILE … ENDWHILE (may run zero times)
Post-condition loopREPEAT … 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)
PassingEffect
BYVALA copy of the value is passed; changes inside do not affect the original
BYREFThe 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

  1. Function vs procedure: a function returns a value.
  2. BYREF is needed when the subroutine must change the caller’s variable.
  3. Choose the right loop and justify it (e.g. REPEAT for input validation).
  4. Use the exact built-in function names from the insert: MID, LENGTH, UCASE, MOD, DIV.
  5. 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.

1. A constant…
2. Which runs zero or more times?
3. FOR loops are…
4. CASE is a form of…
5. UCASE("cat") returns…
6. RIGHT("Revision", 3) returns…
7. A function…
8. A procedure is started with…
9. BYVAL passes…
10. A local variable…
11. A benefit of subroutines is…
12. STR_TO_NUM converts…
13. RAND(10) returns…
14. Which ends a WHILE loop?
15. Why initialise a total to 0?

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.

Was this page helpful?