Calling a function

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

Calling a function

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line runs the function sayHello?
A sayHello();
B sayHello;
2. A function is declared but never called. What happens to its code?
A It does not run
B It runs once automatically
3. After declaring function sayHello(), you write sayHello(); three times. How many times does it run?
A 1 time
B 3 times
4. Which is correct? A function declaration is written first, then the call comes below it.
A function sayHello() { console.log('Hi'); } sayHello();
B function sayHello() { console.log('Hi'); } sayHello
5. True or false: in JavaScript, you can call a function before its declaration appears in the file.
A False
B True

Match the Pairs

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

Left columnRight column
1sayHello(); AThe brackets are missing, so the function is not called and its code does not run. It should be sayHello();
2for (let i = 0; i < 3; i++) { sayHello(); } BsayHello(); sayHello();
3You can call the same function twice to run its code twice Cfunction sayHello() { console.log('Hi!'); } sayHello();
4A function must be declared before it is called, or its declaration must be available when the call runs DThe call inside the loop has no brackets, so sayHello never runs three times. It should be sayHello(); inside the loop.
5sayHello; ECalls the function sayHello, so the code inside it runs once
6for (let i = 0; i < 3; i++) { sayHello; } FCalls sayHello three times by placing the call inside a loop

Definitions

functionName()

Fill in the Blanks

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

1.
function sayHello() { console.log("Hi!"); } ________();
2.
function sayHello() { console.log("Hi!"); } sayHello_);
3.
sayHello(); ________ sayHello() { console.log("Hi!"); }
4.
function cheer() { console.log("Go!"); } ___ (let i = 0; i < 3; i++) cheer();
5.
let n = 0; _____ (n < 2) { sayHello(); n++; }

Spot the Error

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

Question 1
A sayHello();
B sayHello();
C sayHello;
D function sayHello() {
E console.log("Hello!");
F }
Question 2
A sayHelo();
B sayHello();
C function sayHello() {
D console.log("Hello!");
E }
Question 3
A for (let i = 0; i < 3; i++) {
B console.log("Round " + i);
C Cheer();
D }
E function cheer() {
F console.log("Go!"); }
Question 4
A call greet();
B greet();
C function greet() {
D console.log("Hi!");
E }
Question 5
A function sayBye() {
B console.log("Bye!"); }
C sayBye(;
D sayBye();

Answer Key

Calling a function
Section A: Quick Questions
Q1
A — sayHello();
Q2
A — It does not run
Q3
B — 3 times
Q4
A — function sayHello() { console.log('Hi'); } sayHello();
Q5
B — True
Section B: Match the Pairs
Q1
sayHello(); → Calls the function sayHello, so the code inside it runs once
Q2
for (let i = 0; i < 3; i++) { sayHello(); } → Calls sayHello three times by placing the call inside a loop
Q3
You can call the same function twice to run its code twice → sayHello(); sayHello();
Q4
A function must be declared before it is called, or its declaration must be available when the call runs → function sayHello() { console.log('Hi!'); } sayHello();
Q5
sayHello; → The brackets are missing, so the function is not called and its code does not run. It should be sayHello();
Q6
for (let i = 0; i < 3; i++) { sayHello; } → The call inside the loop has no brackets, so sayHello never runs three times. It should be sayHello(); inside the loop.
Section C: Definitions
Q1
functionName() — Calling a function: write its name followed by brackets, like sayHello(), to run its code. You can call it as many times as you like. Without the brackets, it does not run.
Section D: Fill in the Blanks
Q1
function sayHello() { console.log("Hi!"); } sayHello();
Q2
function sayHello() { console.log("Hi!"); } sayHello();
Q3
sayHello(); function sayHello() { console.log("Hi!"); }
Q4
function cheer() { console.log("Go!"); } for (let i = 0; i < 3; i++) cheer();
Q5
let n = 0; while (n < 2) { sayHello(); n++; }
Section E: Spot the Error
Q1
C — Line 3 has no brackets, so the function is not called and nothing prints. It should be sayHello(); and it works even before the declaration.
Q2
A — The function name is spelled wrongly on line 1, so JavaScript can't find it (ReferenceError). It should be sayHello();.
Q3
C — Names are case sensitive. The function is called cheer, not Cheer, so line 3 should be cheer();.
Q4
A — JavaScript has no call keyword. To call a function you write its name and brackets only, so line 1 should be greet();.
Q5
C — The call on line 3 is missing its closing bracket. It should be sayBye(); with both brackets.