Library routines

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Library routines

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What is a library routine?
A A ready-made subroutine supplied with the language
B A subroutine you must always write yourself
2. Which line calls a library routine?
A PROCEDURE ROUND(3.14159, 2)
B Result ← ROUND(3.14159, 2)
3. True or false: you have to write the code for ROUND yourself before using it.
A True
B False
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
5. Which of these is a library routine from the IGCSE course?
A RANDOM()
B RETURNVALUE()

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1ROUND(3.14159, 2) ARoll ← ROUND(RANDOM() * 5, 0) + 1
2LENGTH("Cambridge") BMOD is missing its second value. A library routine such as MOD needs both values, for example 17 MOD 5, which gives 2.
3A library routine is called like a function, so its returned value can be stored in a variable CA library routine call that returns 9, the number of characters in the string
4A library routine supplied with the language gives a random number without the programmer writing the code DThe 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.
5FUNCTION LENGTH(Word : STRING) RETURNS INTEGER RETURN 0 ENDFUNCTION ETotal ← ROUND(Price * 1.2, 2)
6Answer ← 17 MOD FA library routine call that returns 3.14, the number rounded to 2 decimal places

Definitions

library routine

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
Result ← _____(3.14159, 2)
2.
OUTPUT ______("Cloodit")
3.
Dice ← ROUND(______() * 6, 0)
4.
Remainder ← 17 ___ 5
5.
Whole ← 17 ___ 5

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A Word ← "computer"
B Size ← LENGTH Word
C OUTPUT Size
Question 2
A Rem ← MOD(17 5)
B OUTPUT Rem
C // Rem is 2
Question 3
A Pi ← ROUND(3.14159)
B OUTPUT Pi
C // Output is 3.14
Question 4
A DECLARE Chance : REAL
B Chance ← RANDOM()
C IF Chance > 5 THEN
D OUTPUT "Heads"
E ENDIF
Question 5
A Total ← 47
B Tens ← DIV(Total, 10)
C Units ← MOD(Total, 10)
D Check ← Tens + 10 + Units
E OUTPUT Check

Answer Key

Library routines
Section A: Quick Questions
Q1
A — A ready-made subroutine supplied with the language
Q2
B — Result ← ROUND(3.14159, 2)
Q3
B — False
Q4
A — They save time and avoid errors because they are already tested
Q5
A — RANDOM()
Section B: Match the Pairs
Q1
ROUND(3.14159, 2) → A library routine call that returns 3.14, the number rounded to 2 decimal places
Q2
LENGTH("Cambridge") → A library routine call that returns 9, the number of characters in the string
Q3
A library routine is called like a function, so its returned value can be stored in a variable → Total ← ROUND(Price * 1.2, 2)
Q4
A library routine supplied with the language gives a random number without the programmer writing the code → Roll ← ROUND(RANDOM() * 5, 0) + 1
Q5
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.
Q6
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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
Result ← ROUND(3.14159, 2)
Q2
OUTPUT LENGTH("Cloodit")
Q3
Dice ← ROUND(RANDOM() * 6, 0)
Q4
Remainder ← 17 MOD 5
Q5
Whole ← 17 DIV 5
Section E: Spot the Error
Q1
B — LENGTH is a library routine, so it is called like a function with the value in brackets. It should be Size ← LENGTH(Word).
Q2
A — The library routine MOD needs two values separated by a comma. It should be Rem ← MOD(17, 5).
Q3
A — ROUND needs two arguments: the value and the number of decimal places. It should be ROUND(3.14159, 2).
Q4
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.
Q5
D — To rebuild 47 from the DIV and MOD results, the tens must be multiplied by 10. It should be Tens * 10 + Units.