Functions with several parameters

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

Functions with several parameters

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which function header correctly declares two parameters?
A function add(a, b) {
B function add(a b) {
2. What does add(2, 3) do when add is function add(a, b) { return a + b; }?
A a is 3 and b is 2, so it returns 23
B a is 2 and b is 3, so it returns 5
3. A function is declared as function greet(name, age). Which call gives the arguments in the right order?
A greet("Mia", 13)
B greet(13, "Mia")
4. For function subtract(a, b) { return a - b; }, which call gives 10 - 4?
A subtract(4, 10)
B subtract(10, 4)
5. For function add(a, b) { return a + b; }, what is the value of b in the call add(5)?
A undefined
B 0

Match the Pairs

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

Left columnRight column
1function add(a, b) { return a + b; } AThe call gives only one argument for a function with two parameters, so b is undefined. It should be add(2, 3).
2add(2, 3) BThe parameters a and b are not separated by a comma. It should be add(a, b).
3Order matters: the first argument goes to the first parameter, so swapping arguments can change the result Cadd(5) // b is undefined
4If you give too few arguments, the missing parameter is undefined DDeclares a function with two parameters, a and b, separated by a comma
5function add(a b) { return a + b; } Efunction say(name, age) { } say(12, "Ava")
6add(2) FCalls add with two arguments: 2 goes to a and 3 goes to b

Definitions

function add(a, b) {

Fill in the Blanks

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

1.
________ add(a, b) { return a + b; }
2.
function add(a, _) { return a + b; }
3.
console.log(add(2, _)); // prints 5
4.
function sub(a, b) { return a - b; } console.log(sub(__, 4)); // prints 6
5.
function show(a, b) { console.log(b); } show(5); // prints _________

Spot the Error

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

Question 1
A function area(w, h) {
B return w * x;
C }
D console.log(area(4, 3));
Question 2
A function add(a b) {
B return a + b;
C }
D console.log(add(2, 3));
Question 3
A function total(price, qty) {
B return price * qty;
C }
D console.log(total(2.5));
Question 4
A function subtract(a, b) {
B // take b away from a
C return b - a;
D }
E console.log(subtract(10, 4));
Question 5
A function label(first, last) {
B return last + ' ' + first;
C }
D let s = label('Ava', 'Lee');

Answer Key

Functions with several parameters
Section A: Quick Questions
Q1
A — function add(a, b) {
Q2
B — a is 2 and b is 3, so it returns 5
Q3
A — greet("Mia", 13)
Q4
B — subtract(10, 4)
Q5
A — undefined
Section B: Match the Pairs
Q1
function add(a, b) { return a + b; } → Declares a function with two parameters, a and b, separated by a comma
Q2
add(2, 3) → Calls add with two arguments: 2 goes to a and 3 goes to b
Q3
Order matters: the first argument goes to the first parameter, so swapping arguments can change the result → function say(name, age) { } say(12, "Ava")
Q4
If you give too few arguments, the missing parameter is undefined → add(5) // b is undefined
Q5
function add(a b) { return a + b; } → The parameters a and b are not separated by a comma. It should be add(a, b).
Q6
add(2) → The call gives only one argument for a function with two parameters, so b is undefined. It should be add(2, 3).
Section C: Definitions
Q1
function add(a, b) { — Separate several parameters with commas. A call like add(2, 3) must give one argument for each, in the same order. A missing argument becomes undefined.
Section D: Fill in the Blanks
Q1
function add(a, b) { return a + b; }
Q2
function add(a, b) { return a + b; }
Q3
console.log(add(2, 3)); // prints 5
Q4
function sub(a, b) { return a - b; } console.log(sub(10, 4)); // prints 6
Q5
function show(a, b) { console.log(b); } show(5); // prints undefined
Section E: Spot the Error
Q1
B — The function only has the parameters w and h, so x is not defined. It should be return w * h;
Q2
A — Several parameters must be separated by a comma. It should be function add(a, b) {
Q3
D — The call gives only one argument for two parameters, so qty is undefined and the answer is NaN. It should be total(2.5, 4).
Q4
C — The order matters. This takes a away from b, so subtract(10, 4) gives -6. It should be return a - b;
Q5
B — The parameters are used in the wrong order, so the name comes out as 'Lee Ava'. It should be return first + ' ' + last;