Functions – Default Parameters

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

Functions – Default Parameters

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Does a default parameter provide a fallback value when no argument is passed?
A Yes
B No
2. Is = used to assign a default value to a parameter?
A Yes
B No
3. Is ':' a valid way to assign a default parameter value?
A Yes
B No
4. Does passing an argument override a parameter's default value?
A No
B Yes
5. Should 'let' be added before a parameter name in the list?
A No
B Yes

Match the Pairs

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

Left columnRight column
1function greet(name = "Guest") { ... } AUses "Guest" as name if no argument is passed
2function add(a, b = 0) { ... } Bgreet("Sam")
3Provides a fallback value when no argument is given CUses 0 as b if no argument is passed for b
4Overrides the default value when an argument is passed DWrong - default parameters use =, not ==
5function add(a, b == 0) { ... } Ename = "Guest"
6function mul(a, b: 1) { ... } FWrong - default parameters use =, not :

Definitions

=

Fill in the Blanks

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

1.
function greet(name _ "Guest") {
2.
function add(a, b _ 0) {
3.
function power(base, exp = _) {
4.
function counter(step _ 1) {
5.
function scale(factor = _) {

Spot the Error

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

Question 1
A function add(a, b == 0) {
B console.log(a + b);
C }
Question 2
A function mul(a, b: 1) {
B console.log(a * b);
C }
Question 3
A console.log("hi");
B function greet(n "Al") {
C console.log(n);
Question 4
A console.log("go");
B function power(let x = 2) {
C console.log(x);
Question 5
A let a = 1;
B console.log(a);
C Function timer(s = 30) {
D console.log(s);

Answer Key

Functions – Default Parameters
Section A: Quick Questions
Q1
A — Yes
Q2
A — Yes
Q3
B — No
Q4
B — Yes
Q5
A — No
Section B: Match the Pairs
Q1
function greet(name = "Guest") { ... } → Uses "Guest" as name if no argument is passed
Q2
function add(a, b = 0) { ... } → Uses 0 as b if no argument is passed for b
Q3
Provides a fallback value when no argument is given → name = "Guest"
Q4
Overrides the default value when an argument is passed → greet("Sam")
Q5
function add(a, b == 0) { ... } → Wrong - default parameters use =, not ==
Q6
function mul(a, b: 1) { ... } → Wrong - default parameters use =, not :
Section C: Definitions
Q1
= — Assigns a default value to a parameter, used only when no argument is passed for it.
Section D: Fill in the Blanks
Q1
function greet(name = "Guest") {
Q2
function add(a, b = 0) {
Q3
function power(base, exp = 2) {
Q4
function counter(step = 1) {
Q5
function scale(factor = 5) {
Section E: Spot the Error
Q1
A — Default parameters use =, not ==, to assign the value.
Q2
A — Default parameters use =, not :, to assign the value.
Q3
B — Default parameters need an = before the value.
Q4
B — Don't add 'let' inside the parameter list.
Q5
C — 'function' must be lowercase - a capital F causes an error.