Naming functions with verbs

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

Naming functions with verbs

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which is the better name for a function that adds up a shopping bill?
A calculateTotal
B total
2. Which function name is written correctly in JavaScript?
A print menu
B printMenu
3. Which name starts with a verb?
A drawSquare
B squareDrawing
4. A function checks if a player has won. Which name is more meaningful?
A checkWinner
B doStuff
5. Which function name uses camelCase correctly?
A GetUsername_Now
B getUserName

Match the Pairs

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

Left columnRight column
1function calculateTotal(price, qty) { Afunction checkPassword(pass) {
2function printMenu() { BA good function name: it starts with the verb calculate and clearly says the function works out a total.
3Start a function name with a verb so it describes an action CThe function name has a space in it, so JavaScript sees two separate words. Use calculateTotal instead.
4Write the function name in camelCase with no spaces, capitalising each new word after the first DThe name x is not meaningful and has no verb. A better name would be addNumbers.
5function calculate total(price, qty) { Efunction getHighScore() {
6function x(a, b) { FA good function name: it starts with the verb print and names what is printed, the menu.

Definitions

calculateTotal

Fill in the Blanks

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

1.
________ calculateTotal(a, b) { return a + b; }
2.
function showScore() { console.log(10); } _________();
3.
function calculateTotal(a, b) {return a+b;} let total = calculate_____(5, 3);
4.
function _________Area(w, h) {return w*h;} calculateArea(3, 4);
5.
function _________() { console.log("Hi"); } printMenu();

Spot the Error

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

Question 1
A let score1 = 5;
B function add Scores(a, b) {
C return a + b;
D }
Question 2
A function print_menu() {
B console.log('Pizza');
C }
D printMenu();
Question 3
A let player = 'Mia';
B let level = 3;
C function level(n) {
D console.log(n);
E }
F showLevel(level);
Question 4
A let price = 20;
B function ApplyDiscount(p) {
C return p * 0.9;
D }
Question 5
A function isAdult(age) {
B return age >= 18;
C }
D console.log(isadult(20));

Answer Key

Naming functions with verbs
Section A: Quick Questions
Q1
A — calculateTotal
Q2
B — printMenu
Q3
A — drawSquare
Q4
A — checkWinner
Q5
B — getUserName
Section B: Match the Pairs
Q1
function calculateTotal(price, qty) { → A good function name: it starts with the verb calculate and clearly says the function works out a total.
Q2
function printMenu() { → A good function name: it starts with the verb print and names what is printed, the menu.
Q3
Start a function name with a verb so it describes an action → function checkPassword(pass) {
Q4
Write the function name in camelCase with no spaces, capitalising each new word after the first → function getHighScore() {
Q5
function calculate total(price, qty) { → The function name has a space in it, so JavaScript sees two separate words. Use calculateTotal instead.
Q6
function x(a, b) { → The name x is not meaningful and has no verb. A better name would be addNumbers.
Section C: Definitions
Q1
calculateTotal — A function name is a camelCase verb phrase that says what the function does, like calculateTotal or printMenu. Start with a verb, make it meaningful and use no spaces.
Section D: Fill in the Blanks
Q1
function calculateTotal(a, b) { return a + b; }
Q2
function showScore() { console.log(10); } showScore();
Q3
function calculateTotal(a, b) {return a+b;} let total = calculateTotal(5, 3);
Q4
function calculateArea(w, h) {return w*h;} calculateArea(3, 4);
Q5
function printMenu() { console.log("Hi"); } printMenu();
Section E: Spot the Error
Q1
B — A function name cannot contain spaces. Write the words together in camelCase: addScores.
Q2
A — Function names use camelCase, not underscores. It should be printMenu, which also matches the call on line 4.
Q3
C — The name 'level' is a noun and does not say what the function does. Start with a verb: showLevel.
Q4
B — camelCase names start with a lowercase letter. It should be applyDiscount.
Q5
D — The call must use the exact camelCase name. isadult is not defined; it should be isAdult with a capital A.