Topic 8 ยท Programming ยท 8.13

Library Routines Explained Simply

Library routines are ready-made tools. You call the correct routine, give it the required values and use the result.

Invitation

Why write code that already exists?

Programs often need to perform common jobs such as finding a remainder, carrying out integer division, rounding a value or creating a random number.

A library routine is a ready-made piece of code provided by the programming language. You call it by name instead of writing the whole process yourself.

Choose the tool. Call it correctly. Use the returned result.
Figure 1
The ready-made toolbox
MODRemainder
DIVWhole-number answer
ROUNDRounded value
RANDOMRandom value
You do not build the tool. You select and use it.
Big Idea

Each routine has one precise job

MODReturns the remainder
DIVReturns the whole-number result
ROUNDRounds to a stated number of places
RANDOMGenerates a random value
Exam rule: Name the routine and state exactly what result it returns.
FutureLogic Bridge

Library routines are tools in a toolbox

Imagine a toolbox containing four specialist tools. A screwdriver cannot replace a hammer. In the same way, MOD cannot replace DIV.

Look at Figure 2. The program identifies the job, selects the correct tool and receives a result.

The question describes the job. Your answer chooses the matching routine.
Figure 2
Choose the correct tool
1 ยท What result is needed?
โ†“
2 ยท Select MOD, DIV, ROUND or RANDOM
โ†“
3 ยท Store or output the result
Worked Example

Use the four routines accurately

PseudocodeMOD and DIV
Remainder โ† MOD(17, 5)
Whole โ† DIV(17, 5)

OUTPUT Remainder
OUTPUT Whole
PythonSame results
remainder = 17 % 5
whole = 17 // 5

print(remainder)
print(whole)

Follow the calculation

17 divided by 5 gives 3 whole groups with 2 left over.

DIV(17, 5) = 3   |   MOD(17, 5) = 2

PseudocodeROUND
Answer โ† ROUND(7.486, 2)
OUTPUT Answer
PythonROUND
answer = round(7.486, 2)
print(answer)
Both examples produce 7.49.
Random Numbers

Read the required range carefully

RANDOM produces a value that is not predictable. The exact syntax depends on the language and the range required.

In pseudocode, a common pattern for a random integer from 1 to 20 is:

Pseudocode1 to 20
Number โ† ROUND(RANDOM() * 19, 0) + 1
Stay focused: Check whether the question wants a decimal, a whole number or a specific range.
Figure 3
Building the range
RANDOM() โ†’ random decimal
โ†“
ร— 19 โ†’ scale the value
โ†“
ROUND and + 1 โ†’ 1 to 20
Exam Tip

Precision earns the marks

Library-routine questions often award separate marks for the correct routine, the correct arguments and the complete statement.

For a 3- or 4-mark answer: use the named routine, include the correct values inside the brackets, store or output the result, and match the question exactly.

Focused answer

Question: Output the average rounded to one decimal place.

OUTPUT ROUND(Total / Count, 1)

Common Mistake

Do not describe one routine using another

Incorrect: โ€œMOD divides two numbers.โ€
Correct: โ€œMOD returns the remainder after division.โ€

Another common mistake is writing only the calculation when the question asks for an output statement.

Incomplete: ROUND(Total / Count, 1)
Complete: OUTPUT ROUND(Total / Count, 1)
Summary

The four tools to remember

MODRemainder after division
DIVWhole-number result
ROUNDRounded value
RANDOM generates a random value. Always read the required range carefully.