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. Can a function call another function inside its body?
A Yes. A function body can contain calls to other functions.
B No. Only code outside functions can call functions.
2. Which function is a helper function? function square(n) { return n * n; } function area(side) { return square(side); }
A square, because area calls it to do a small job
B area, because it has a longer name
3. Which design follows 'one job per function'?
A getTotal() adds the prices and showTotal() prints the result
B doEverything() adds prices, prints, asks for input and checks errors
4. A helper function returns a value. How does the calling function get that value?
A helper(5) = result;
B let result = helper(5);
5. Which function body correctly calls the helper double()?
A function quadruple(n) { return double double n; }
B function quadruple(n) { return double(double(n)); }

Match the Pairs

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

Left columnRight column
1function area(w, h) { return w * h; } Afunction total(a, b) { return double(a) + double(b); }
2function showArea(w, h) { console.log(area(w, h)); } BA small helper function that does one job: it returns the area of a rectangle.
3A helper function returns a value, and the calling function uses that value in a calculation. Cfunction main() { greet(); playGame(); sayBye(); }
4A main program calls several small functions in order, one job each. DA function that calls the helper area() inside its body and prints the result.
5function showArea(w, h) { console.log(area); } EThe helper area() has no return, so it gives back undefined and showArea prints undefined.
6function area(w, h) { w * h; } function showArea(w, h) { console.log(area(w, h)); } FThe helper is named but never called. It needs brackets and arguments: area(w, h).

Definitions

helper function

Fill in the Blanks

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

1.
function twice(n) { return n * 2; } function four(n) { return _____(twice(n)); }
2.
function isEven(n) { return n % 2 === 0; } function show(n) { console.log(______(n)); }
3.
let name = getName(); let msg = makeGreeting(name); showMessage(___);
4.
function sumTwice(a, b) { ______ twice(a) + twice(b); }
5.
function entry(age) { __ (isAdult(age)) { console.log("In"); } }

Spot the Error

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

Question 1
A function square(n) {
B return n * n;
C }
D function sumSquares(a, b {
E return square(a) + square(b);
F }
Question 2
A function getName() {
B console.log("Sam");
C }
D function welcome() {
E console.log("Hi " + getName());
F }
Question 3
A function area(w h) {
B return w * h;
C }
D function showArea(w, h) {
E console.log(area(w, h));
F }
Question 4
A function sq(n) {return n*n;}
B function sumSq(a, b) {
C return sq(a + b);
D }
Question 5
A function big(n) {return n>9;}
B function check(n) {
C if (big) {
D console.log("Big");
E }
F }

Answer Key

Functions calling other functions
Section A: Quick Questions
Q1
A — Yes. A function body can contain calls to other functions.
Q2
A — square, because area calls it to do a small job
Q3
A — getTotal() adds the prices and showTotal() prints the result
Q4
B — let result = helper(5);
Q5
B — function quadruple(n) { return double(double(n)); }
Section B: Match the Pairs
Q1
function area(w, h) { return w * h; } → A small helper function that does one job: it returns the area of a rectangle.
Q2
function showArea(w, h) { console.log(area(w, h)); } → A function that calls the helper area() inside its body and prints the result.
Q3
A helper function returns a value, and the calling function uses that value in a calculation. → function total(a, b) { return double(a) + double(b); }
Q4
A main program calls several small functions in order, one job each. → function main() { greet(); playGame(); sayBye(); }
Q5
function showArea(w, h) { console.log(area); } → The helper is named but never called. It needs brackets and arguments: area(w, h).
Q6
function area(w, h) { w * h; } function showArea(w, h) { console.log(area(w, h)); } → The helper area() has no return, so it gives back undefined and showArea prints undefined.
Section C: Definitions
Q1
helper function — A small function that does one job and is called from inside another function, so a big task is built from small, reusable parts.
Section D: Fill in the Blanks
Q1
function twice(n) { return n * 2; } function four(n) { return twice(twice(n)); }
Q2
function isEven(n) { return n % 2 === 0; } function show(n) { console.log(isEven(n)); }
Q3
let name = getName(); let msg = makeGreeting(name); showMessage(msg);
Q4
function sumTwice(a, b) { return twice(a) + twice(b); }
Q5
function entry(age) { if (isAdult(age)) { console.log("In"); } }
Section E: Spot the Error
Q1
D — The closing bracket after the parameter list is missing. It should be function sumSquares(a, b) {. The function can then call the square() helper twice.
Q2
B — A helper that gives a value to another function must use return. console.log only prints, so getName() gives back undefined and welcome() prints Hi undefined. It should be return "Sam";
Q3
A — Parameters must be separated by a comma. It should be function area(w, h) {. Then showArea can call area() as a helper.
Q4
C — The helper sq() must be called on each number separately and the results added. sq(a + b) adds first and then squares, which gives a different answer. It should be return sq(a) + sq(b);
Q5
C — The helper must be called with brackets and an argument. Without them, big is just the function itself and is always truthy. It should be if (big(n)) {