Library routines inside your own subroutines

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

Library routines inside your own subroutines

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

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
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
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
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)
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

Match the Pairs

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

Left columnRight column
1RETURN Number MOD 2 = 0 AOUTPUT ROUND(CalculateTotal(Price), 2)
2RETURN ROUND(Price * 1.1, 2) BFUNCTION RollDie() RETURNS INTEGER RETURN ROUND(RANDOM() * 5, 0) + 1 ENDFUNCTION
3A function called RollDie uses the RANDOM library routine and returns a whole number from 1 to 6 CThe result of ROUND is not returned. The function must use RETURN so the rounded value is passed back to the caller.
4A user-defined function is used beside a library routine in one statement, by passing the function's result into ROUND DDIV 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.
5FUNCTION IsEven(Number : INTEGER) RETURNS BOOLEAN RETURN Number DIV 2 = 0 ENDFUNCTION EInside IsEven, this uses MOD to find the remainder after dividing by 2 and returns TRUE when the remainder is 0
6FUNCTION RoundPrice(Price : REAL) RETURNS REAL ROUND(Price, 2) ENDFUNCTION FInside a function, this works out the price plus 10 percent and returns it rounded to 2 decimal places

Definitions

RETURN ROUND(Price, 2)

Fill in the Blanks

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

1.
FUNCTION IsEven(N : INTEGER) RETURNS BOOLEAN RETURN N ___ 2 = 0 ENDFUNCTION
2.
FUNCTION RollDie() RETURNS INTEGER RETURN ROUND(______() * 5, 0) + 1 ENDFUNCTION
3.
FUNCTION Tidy(Price : REAL) RETURNS REAL RETURN ROUND(Price, _) ENDFUNCTION
4.
Result ← _____(AddTax(Price), 2) OUTPUT Result
5.
FUNCTION Tens(N : INTEGER) RETURNS INTEGER RETURN N ___ 10 ENDFUNCTION

Spot the Error

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

Question 1
A FUNCTION Roll RETURN INTEGER
B RETURN ROUND(RANDOM()*5,0)+1
C ENDFUNCTION
Question 2
A PROCEDURE Fix(P) RETURNS REAL
B RETURN ROUND(P, 2)
C ENDFUNCTION
Question 3
A FUNCTION Fix(P) RETURNS REAL
B NewP ← ROUND(P, 2)
C RETURN P
D ENDFUNCTION
Question 4
A Num ← 14
B IF IsEven THEN
C OUTPUT "Even"
D ENDIF
Question 5
A PROCEDURE ShowTime(Secs)
B Mins ← Secs MOD 60
C OUTPUT Mins
D ENDPROCEDURE

Answer Key

Library routines inside your own subroutines
Section A: Quick Questions
Q1
B — RETURN Number MOD 2 = 0
Q2
A — Yes: RETURN ROUND(Price, 2) uses the value that ROUND gives back
Q3
A — FUNCTION IsEven(Number : INTEGER) RETURNS BOOLEAN
Q4
B — RETURN ROUND(Price, 2)
Q5
A — RETURN ROUND(RANDOM() * 5, 0) + 1
Section B: Match the Pairs
Q1
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
Q2
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
Q3
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
Q4
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)
Q5
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.
Q6
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.
Section C: Definitions
Q1
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.
Section D: Fill in the Blanks
Q1
FUNCTION IsEven(N : INTEGER) RETURNS BOOLEAN RETURN N MOD 2 = 0 ENDFUNCTION
Q2
FUNCTION RollDie() RETURNS INTEGER RETURN ROUND(RANDOM() * 5, 0) + 1 ENDFUNCTION
Q3
FUNCTION Tidy(Price : REAL) RETURNS REAL RETURN ROUND(Price, 2) ENDFUNCTION
Q4
Result ← ROUND(AddTax(Price), 2) OUTPUT Result
Q5
FUNCTION Tens(N : INTEGER) RETURNS INTEGER RETURN N DIV 10 ENDFUNCTION
Section E: Spot the Error
Q1
A — The keyword in a function header is RETURNS, not RETURN. The header should be FUNCTION Roll RETURNS INTEGER.
Q2
A — A procedure does not return a value. The subroutine uses RETURN with ROUND, so it must be a FUNCTION.
Q3
C — This returns the original price and ignores the rounded value. It should be RETURN NewP so the result of ROUND is sent back.
Q4
B — IsEven needs the number to test in brackets when it is called. It should be IF IsEven(Num) THEN.
Q5
B — MOD gives the remainder, not the number of whole minutes. Whole minutes need DIV: Mins ← Secs DIV 60.