← IGCSE Computer Science Paper 2

Library routines inside your own subroutines

Your own subroutine can call a library routine such as ROUND, MOD, DIV or RANDOM in its statements and use the value it gives back, for example in a RETURN.

Understanding Library routines inside your own subroutines in IGCSE Computer Science Paper 2

What it does: RollDie calls RANDOM, which gives a number between 0 and 1. Multiplying by 5, rounding and adding 1 gives a whole number from 1 to 6, and RETURN sends it back. CalculateTotal is the programmer's own function and ROUND is a library routine. The value from the function call is passed into ROUND in the same statement. Number MOD 2 = 0 is either TRUE or FALSE, so the function returns a BOOLEAN even though MOD itself works on integers. ROUND takes the value to round first, then the number of decimal places. ROUND(Price, 2) rounds Price to 2 places.

Common mistakes: The keyword in a function header is RETURNS, not RETURN. The header should be FUNCTION Roll RETURNS INTEGER. A procedure does not return a value. The subroutine uses RETURN with ROUND, so it must be a FUNCTION.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 429–438) and word search (Modules 419–438).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Your function IsEven must use a library routine to test for an even number. Which RETURN line is correct?
RETURN Number MOD 2 = 0RETURN Number DIV 2 = 0
Matches
FUNCTION IsEven(Number : INTEGER) RETURNS BOOLEAN RETURN Number DIV 2 = 0 ENDFUNCTION
↓
DIV is used instead of MOD. DIV gives the whole-number quotient, not the remainder, so the test does not check whether the number is even.
Applying
Meanings
Term
RETURN ROUND(Price, 2)
Meaning
Your own subroutine can call a library routine such as ROUND, MOD, DIV or RANDOM in its statements and use the value it gives back, for example in a RETURN.
Practice
FUNCTION IsEven(Number : INTEGER) RETURNS BOOLEAN
Testing
Gaps
RETURN N 2 = 0
Check Answer
Spots
1FUNCTION Roll RETURN INTEGER
2 RETURN ROUND(RANDOM()*5,0)+1

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Giant Squid' badge — Nested Navigator, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Your function IsEven must use a library routine to test for an even number. Which RETURN line is correct?
A. RETURN Number DIV 2 = 0
B. RETURN Number MOD 2 = 0 ✓
MOD gives the remainder. An even number leaves remainder 0 when divided by 2. DIV gives the whole-number quotient, which is not the test we need.
2. Can a function you write call a library routine such as ROUND inside its own statements?
A. Yes: RETURN ROUND(Price, 2) uses the value that ROUND gives back ✓
B. No: only the main program can call ROUND
Your own procedures and functions can call library routines such as ROUND, MOD, DIV and RANDOM, and use the value they give back.
3. Which header is correct for IsEven, which uses MOD to test a whole number?
A. FUNCTION IsEven(Number : INTEGER) RETURNS BOOLEAN ✓
B. FUNCTION IsEven(Number : INTEGER) RETURNS INTEGER
Number MOD 2 = 0 is either TRUE or FALSE, so the function returns a BOOLEAN even though MOD itself works on integers.
4. Inside RoundPrice, which statement rounds Price to 2 decimal places and gives it back?
A. RETURN ROUND(2, Price)
B. RETURN ROUND(Price, 2) ✓
ROUND takes the value to round first, then the number of decimal places. ROUND(Price, 2) rounds Price to 2 places.
5. FUNCTION RollDie RETURNS INTEGER must give a whole number from 1 to 6. Which RETURN line fits?
A. RETURN ROUND(RANDOM() * 5, 0) + 1 ✓
B. RETURN RANDOM() * 6
RANDOM() gives a decimal between 0 and 1. ROUND turns the scaled value into a whole number. RANDOM() * 6 alone is still a decimal, so it cannot be an INTEGER.

Match the Pairs

RETURN Number MOD 2 = 0 → Inside IsEven, this uses MOD to find the remainder after dividing by 2 and returns TRUE when the remainder is 0
RETURN ROUND(Price * 1.1, 2) → Inside a function, this works out the price plus 10 percent and returns it rounded to 2 decimal places
A function called RollDie uses the RANDOM library routine and returns a whole number from 1 to 6 → FUNCTION RollDie() RETURNS INTEGER RETURN ROUND(RANDOM() * 5, 0) + 1 ENDFUNCTION
A user-defined function is used beside a library routine in one statement, by passing the function's result into ROUND → OUTPUT ROUND(CalculateTotal(Price), 2)
FUNCTION IsEven(Number : INTEGER) RETURNS BOOLEAN RETURN Number DIV 2 = 0 ENDFUNCTION → DIV is used instead of MOD. DIV gives the whole-number quotient, not the remainder, so the test does not check whether the number is even.
FUNCTION RoundPrice(Price : REAL) RETURNS REAL ROUND(Price, 2) ENDFUNCTION → The result of ROUND is not returned. The function must use RETURN so the rounded value is passed back to the caller.

Key Term

RETURN ROUND(Price, 2)
Your own subroutine can call a library routine such as ROUND, MOD, DIV or RANDOM in its statements and use the value it gives back, for example in a RETURN.

Fill in the Blanks

1. FUNCTION IsEven(N : INTEGER) RETURNS BOOLEAN RETURN N MOD 2 = 0 ENDFUNCTION
Hint: Type the library operator that gives the remainder after a division.
2. FUNCTION RollDie() RETURNS INTEGER RETURN ROUND(RANDOM() * 5, 0) + 1 ENDFUNCTION
Hint: Type the library routine that gives a random number between 0 and 1.
3. FUNCTION Tidy(Price : REAL) RETURNS REAL RETURN ROUND(Price, 2) ENDFUNCTION
Hint: Type the number of decimal places to round a price to (pounds and pence).
4. Result ← ROUND(AddTax(Price), 2) OUTPUT Result
Hint: Type the library routine that wraps around the user-written function call to set the decimal places.
5. FUNCTION Tens(N : INTEGER) RETURNS INTEGER RETURN N DIV 10 ENDFUNCTION
Hint: Type the library operator that gives the whole-number part of a division.

Spot the Error

Question 1
A FUNCTION Roll RETURN INTEGER
B RETURN ROUND(RANDOM()*5,0)+1
C ENDFUNCTION
Line A — The keyword in a function header is RETURNS, not RETURN. The header should be FUNCTION Roll RETURNS INTEGER.
Question 2
A PROCEDURE Fix(P) RETURNS REAL
B RETURN ROUND(P, 2)
C ENDFUNCTION
Line A — A procedure does not return a value. The subroutine uses RETURN with ROUND, so it must be a FUNCTION.
Question 3
A FUNCTION Fix(P) RETURNS REAL
B NewP ← ROUND(P, 2)
C RETURN P
D ENDFUNCTION
Line C — This returns the original price and ignores the rounded value. It should be RETURN NewP so the result of ROUND is sent back.
Question 4
A Num ← 14
B IF IsEven THEN
C OUTPUT "Even"
D ENDIF
Line B — IsEven needs the number to test in brackets when it is called. It should be IF IsEven(Num) THEN.
Question 5
A PROCEDURE ShowTime(Secs)
B Mins ← Secs MOD 60
C OUTPUT Mins
D ENDPROCEDURE
Line B — MOD gives the remainder, not the number of whole minutes. Whole minutes need DIV: Mins ← Secs DIV 60.

Student with a class code? Join your class →