Writing a program that uses functions and control structures

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

Writing a program that uses functions and control structures

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which is the better way to structure a program?
A Write small functions, then call them from a short main section
B Put every step in one long block with no functions
2. Which function takes a parameter and returns a value?
A function double(n) { return n * 2; }
B function double() { console.log(2); }
3. Where is the best place for a comment that explains a function?
A Hidden at the very end of the program
B Directly above the function declaration
4. Which main section calls the function correctly?
A let result = double(5); console.log(result);
B let result = double; console.log(result);
5. How should you test a function in a larger program?
A Run the whole program once and hope it all works
B Call it on its own with known inputs and check the output

Match the Pairs

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

Left columnRight column
1function isEven(n) { return n % 2 === 0; } AA separate test that calls one function with a known input and compares the output with the expected answer.
2console.log(double(4)); // expect 8 BThe function has no return statement, so it gives back undefined and the main section prints undefined instead of 8.
3The main section is short. It calls the functions and lets them do the work. CA small function that takes a parameter n and returns true or false so the main section can use it in an if statement.
4A comment above a function explains what the function does, what it takes in and what it returns. D// Returns the area of a rectangle function area(w, h) { return w * h; }
5function double(n) { n * 2; } console.log(double(4)); EThe function is never called because the brackets and the argument are missing, so result holds the function and not the answer.
6function double(n) { return n * 2; } let result = double; Flet total = addUp(5); console.log(total);

Definitions

function double(n) { return n * 2; }

Fill in the Blanks

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

1.
________ double(n) { return n * 2; }
2.
// Doubles a number function double(n) { ______ n * 2; }
3.
function double(n) { return n * 2; } let result = ______(5); console.log(result);
4.
function isEven(n) { return n % 2 ___ 0; } console.log(isEven(4));
5.
function double(n) { return n * 2; } ___ (let i = 1; i <= 3; i++) { console.log(double(i)); }

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 console.log(squar(5));
Question 2
A // Checks if a number is even
B func isEven(n) {
C return n % 2 === 0;
D }
Question 3
A function greet(name {
B return 'Hi ' + name;
C }
D console.log(greet('Sam'));
Question 4
A function area(w, h) {
B return w * h;
C }
D console.log(area(5));
Question 5
A function sign(n) {
B if (n < 0) {
C retrun 'negative';
D }
E return 'positive';
F }

Answer Key

Writing a program that uses functions and control structures
Section A: Quick Questions
Q1
A — Write small functions, then call them from a short main section
Q2
A — function double(n) { return n * 2; }
Q3
B — Directly above the function declaration
Q4
A — let result = double(5); console.log(result);
Q5
B — Call it on its own with known inputs and check the output
Section B: Match the Pairs
Q1
function isEven(n) { return n % 2 === 0; } → A small function that takes a parameter n and returns true or false so the main section can use it in an if statement.
Q2
console.log(double(4)); // expect 8 → A separate test that calls one function with a known input and compares the output with the expected answer.
Q3
The main section is short. It calls the functions and lets them do the work. → let total = addUp(5); console.log(total);
Q4
A comment above a function explains what the function does, what it takes in and what it returns. → // Returns the area of a rectangle function area(w, h) { return w * h; }
Q5
function double(n) { n * 2; } console.log(double(4)); → The function has no return statement, so it gives back undefined and the main section prints undefined instead of 8.
Q6
function double(n) { return n * 2; } let result = double; → The function is never called because the brackets and the argument are missing, so result holds the function and not the answer.
Section C: Definitions
Q1
function double(n) { return n * 2; } — Build a program from small functions that take parameters and return values, then call them from a short main section. Add a comment above each function and test each one separately.
Section D: Fill in the Blanks
Q1
function double(n) { return n * 2; }
Q2
// Doubles a number function double(n) { return n * 2; }
Q3
function double(n) { return n * 2; } let result = double(5); console.log(result);
Q4
function isEven(n) { return n % 2 === 0; } console.log(isEven(4));
Q5
function double(n) { return n * 2; } for (let i = 1; i <= 3; i++) { console.log(double(i)); }
Section E: Spot the Error
Q1
D — The call has a spelling mistake. The function is declared as square, so squar is not defined and causes a ReferenceError. It should be console.log(square(5));
Q2
B — A JavaScript function is declared with the keyword function, not func. The line should be function isEven(n) {
Q3
A — The closing bracket after the parameter name is missing, so the brackets are not paired. The line should be function greet(name) {
Q4
D — area needs two arguments but the call gives only one. h is undefined, so the result is NaN. The call should be area(5, 3).
Q5
C — The keyword return is spelt wrongly, so JavaScript gives a syntax error. The line should be return 'negative';