← IGCSE Computer Science Paper 2

Library routines

A ready-made subroutine supplied with the language that you can call without writing it, such as ROUND, RANDOM or LENGTH. It saves time and avoids errors.

Understanding Library routines in IGCSE Computer Science Paper 2

What it does: The library routine ROUND is called inside an expression and its returned value is stored in Total, just like a user-written function. RANDOM is a ready-made routine that returns a number between 0 and 1. Using it saves time and avoids errors compared with writing your own. Library routines are written and tested already, so programmers save time and are less likely to introduce errors. MOD returns the remainder after division, so it needs a number on each side. Without the second value the expression is incomplete.

Common mistakes: LENGTH is a library routine, so it is called like a function with the value in brackets. It should be Size ← LENGTH(Word). The library routine MOD needs two values separated by a comma. It should be Rem ← MOD(17, 5).

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
What is a library routine?
A ready-made subroutine supplied with the languageA subroutine you must always write yourself
Matches
FUNCTION LENGTH(Word : STRING) RETURNS INTEGER RETURN 0 ENDFUNCTION
↓
The programmer wrote their own LENGTH routine and it does not work. LENGTH is already a library routine, so there is no need to write it.
Applying
Meanings
Term
library routine
Meaning
A ready-made subroutine supplied with the language that you can call without writing it, such as ROUND, RANDOM or LENGTH. It saves time and avoids errors.
Practice
Word ← "Computer"
Testing
Gaps
Result ← (3.14159, 2)
Check Answer
Spots
1Word ← "computer"
2Size ← LENGTH Word

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

The 'Puffin' badge — Definition Diver, 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. What is a library routine?
A. A ready-made subroutine supplied with the language ✓
B. A subroutine you must always write yourself
A library routine is already written and supplied with the programming language, so you can call it without writing it.
2. Which line calls a library routine?
A. PROCEDURE ROUND(3.14159, 2)
B. Result ← ROUND(3.14159, 2) ✓
ROUND is called like a function. The second line starts to define a new procedure, which is not a call at all.
3. True or false: you have to write the code for ROUND yourself before using it.
A. True
B. False ✓
ROUND is a library routine. It is already provided, so you only need to call it.
4. Which is a benefit of using library routines?
A. They save time and avoid errors because they are already tested ✓
B. They make the program run without needing any inputs
Library routines are written and tested already, so programmers save time and are less likely to introduce errors.
5. Which of these is a library routine from the IGCSE course?
A. RANDOM() ✓
B. RETURNVALUE()
RANDOM() gives a random number between 0 and 1. RETURNVALUE() is not a library routine in the course.

Match the Pairs

ROUND(3.14159, 2) → A library routine call that returns 3.14, the number rounded to 2 decimal places
LENGTH("Cambridge") → A library routine call that returns 9, the number of characters in the string
A library routine is called like a function, so its returned value can be stored in a variable → Total ← ROUND(Price * 1.2, 2)
A library routine supplied with the language gives a random number without the programmer writing the code → Roll ← ROUND(RANDOM() * 5, 0) + 1
FUNCTION LENGTH(Word : STRING) RETURNS INTEGER RETURN 0 ENDFUNCTION → The programmer wrote their own LENGTH routine and it does not work. LENGTH is already a library routine, so there is no need to write it.
Answer ← 17 MOD → MOD is missing its second value. A library routine such as MOD needs both values, for example 17 MOD 5, which gives 2.

Key Term

library routine
A ready-made subroutine supplied with the language that you can call without writing it, such as ROUND, RANDOM or LENGTH. It saves time and avoids errors.

Fill in the Blanks

1. Result ← ROUND(3.14159, 2)
Hint: Type the library routine that rounds a number to a given number of decimal places.
2. OUTPUT LENGTH("Cloodit")
Hint: Type the library routine that counts how many characters are in a string.
3. Dice ← ROUND(RANDOM() * 6, 0)
Hint: Type the library routine that returns a random number between 0 and 1.
4. Remainder ← 17 MOD 5
Hint: Type the library operation that gives the remainder after a division.
5. Whole ← 17 DIV 5
Hint: Type the library operation that gives only the whole-number part of a division.

Spot the Error

Question 1
A Word ← "computer"
B Size ← LENGTH Word
C OUTPUT Size
Line B — LENGTH is a library routine, so it is called like a function with the value in brackets. It should be Size ← LENGTH(Word).
Question 2
A Rem ← MOD(17 5)
B OUTPUT Rem
C // Rem is 2
Line A — The library routine MOD needs two values separated by a comma. It should be Rem ← MOD(17, 5).
Question 3
A Pi ← ROUND(3.14159)
B OUTPUT Pi
C // Output is 3.14
Line A — ROUND needs two arguments: the value and the number of decimal places. It should be ROUND(3.14159, 2).
Question 4
A DECLARE Chance : REAL
B Chance ← RANDOM()
C IF Chance > 5 THEN
D OUTPUT "Heads"
E ENDIF
Line C — RANDOM() gives a number between 0 and 1, so it can never be greater than 5. The test should use a value such as 0.5.
Question 5
A Total ← 47
B Tens ← DIV(Total, 10)
C Units ← MOD(Total, 10)
D Check ← Tens + 10 + Units
E OUTPUT Check
Line D — To rebuild 47 from the DIV and MOD results, the tens must be multiplied by 10. It should be Tens * 10 + Units.

Student with a class code? Join your class →