Functions calling other functions

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

Functions calling other functions

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which function body calls a helper function?
A function area(w, h) { return w * h; }
B function area(w, h) { return multiply(w, h); }
2. True or false: A function can call another function inside its body.
A True
B False
3. Which is the better way to use a helper in a larger function?
A return a * a + b * b; // square() is never used
B return square(a) + square(b);
4. A decomposition has three boxes: getInput, calculate and showResult. How should the code match it?
A One function for each box
B One giant function that does all three jobs
5. Which main function calls the steps in the right order?
A function main() { load(); process(); show(); }
B function main() { show(); process(); load(); }

Match the Pairs

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

Left columnRight column
1return square(a) + square(b); AThe function calls the helper square() twice and adds the results, so square() does the squaring and this function only combines them.
2function main() { getInput(); process(); showResult(); } Bfunction main() { readScores(); average(); report(); }
3A helper function does one small job, and a larger function calls it to get that job done. CA main function that calls each step function in order, one per box of the decomposition.
4Each box of a decomposition becomes its own function, and a main function calls them in order. DThe helper is not called. Writing square without brackets just refers to the function and does not run it, so no result is shown. It should be square(3).
5function hypotenuse(a, b) { return Math.sqrt(square(a) + square(b)) } function square(n) { return n * n } console.log(square); Efunction area(w, h) { return multiply(w, h); }
6function main() { showResult(); calculate(); getInput(); } FThe step functions are called in the wrong order. Input must come first, then the calculation, then showing the result.

Definitions

return square(a) + square(b);

Fill in the Blanks

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

1.
const square = n => n * n; const cube = n => n * ______(n);
2.
function setup() { console.log("Ready"); } function main() { _____(); } main();
3.
const isEven = n => n % 2 === 0; const check = n => ______(n) ? "yes" : "no";
4.
const area = (w, h) => w * h; const cost = (w, h, r) => area(w, h) _ r;
5.
const sum = (a, b) => a + b; const mean = (a, b) => ___(a, b) / 2;

Spot the Error

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

Question 1
A const sq = n => n * n;
B function sumSq(a, b) {
C return sq[a] + sq(b);
D }
Question 2
A function getArea(w, h) {
B return w + h;
C }
D function showArea(w, h) {
E console.log(getArea(w, h));
F }
Question 3
A function greet(name) {
B 'Hi ' + name;
C }
D function welcome(name) {
E console.log(greet(name));
F }
Question 4
A function main() {
B const s = [70, 85, 90];
C const t = total(s);
D show;
E }
F main();
Question 5
A func cube(n) {
B return n * n * n;
C }
D function boxVol(s) {
E return cube(s);
F }

Answer Key

Functions calling other functions
Section A: Quick Questions
Q1
B — function area(w, h) { return multiply(w, h); }
Q2
A — True
Q3
B — return square(a) + square(b);
Q4
A — One function for each box
Q5
A — function main() { load(); process(); show(); }
Section B: Match the Pairs
Q1
return square(a) + square(b); → The function calls the helper square() twice and adds the results, so square() does the squaring and this function only combines them.
Q2
function main() { getInput(); process(); showResult(); } → A main function that calls each step function in order, one per box of the decomposition.
Q3
A helper function does one small job, and a larger function calls it to get that job done. → function area(w, h) { return multiply(w, h); }
Q4
Each box of a decomposition becomes its own function, and a main function calls them in order. → function main() { readScores(); average(); report(); }
Q5
function hypotenuse(a, b) { return Math.sqrt(square(a) + square(b)) } function square(n) { return n * n } console.log(square); → The helper is not called. Writing square without brackets just refers to the function and does not run it, so no result is shown. It should be square(3).
Q6
function main() { showResult(); calculate(); getInput(); } → The step functions are called in the wrong order. Input must come first, then the calculation, then showing the result.
Section C: Definitions
Q1
return square(a) + square(b); — A function can call another function inside its body. Here square() is a helper, so the bigger function stays short and each function does one job.
Section D: Fill in the Blanks
Q1
const square = n => n * n; const cube = n => n * square(n);
Q2
function setup() { console.log("Ready"); } function main() { setup(); } main();
Q3
const isEven = n => n % 2 === 0; const check = n => isEven(n) ? "yes" : "no";
Q4
const area = (w, h) => w * h; const cost = (w, h, r) => area(w, h) * r;
Q5
const sum = (a, b) => a + b; const mean = (a, b) => sum(a, b) / 2;
Section E: Spot the Error
Q1
C — A function is called with round brackets, not square brackets. It should be sq(a), so the helper runs and gives back the square of a.
Q2
B — The helper getArea must multiply width by height. Adding them gives the wrong answer for showArea. It should be return w * h;
Q3
B — The helper greet has no return keyword, so it gives back undefined to welcome. It should be return 'Hi ' + name;
Q4
D — Without round brackets the function show is not called, so nothing happens. The step should be called with show(t); so main runs the steps in order.
Q5
A — The keyword to declare a function is function, not func. The line should be function cube(n) {