Functions – Default Parameters, online
Six activities your students do on their own devices. See who's finished, and their scores.
Learning
Understanding
Testing
Functions – Default Parameters in one minute
What students need to know
This is exactly what a default parameter does. Passing "Sam" replaces the default "Guest" value.
If no argument is given, the default value is used instead. Default parameters use = directly in the parameter list.
Mistakes students make
- Default parameters use =, not ==, to assign the value.
- Default parameters use =, not :, to assign the value.
- Default parameters need an = before the value.
- Don't add 'let' inside the parameter list.
PREVIOUS · MODULE 40Functions – Return Values
YOU ARE HERE · MODULE 41Functions – Default Parameters
NEXT · MODULE 42Arrow Functions
What students will learn in this module
Every question in Module 41
Quick questions
- Does a default parameter provide a fallback value when no argument is passed?AYesBNo
- Is = used to assign a default value to a parameter?AYesBNo
- Is ':' a valid way to assign a default parameter value?AYesBNo
- Does passing an argument override a parameter's default value?ANoBYes
- Should 'let' be added before a parameter name in the list?ANoBYes
Key terms
Students type the meaning of:
=
Match the pairs
function greet(name = "Guest") { ... }Match it to what it doesfunction add(a, b = 0) { ... }Match it to what it doesProvides a fallback value when no argument is givenMatch it to what it doesOverrides the default value when an argument is passedMatch it to what it doesfunction add(a, b == 0) { ... }Match it to what it doesfunction mul(a, b: 1) { ... }Match it to what it doesFill in the blanks
function greet(name _ "Guest") {Hint: Type the symbol used to assign a default value to name.
function add(a, b _ 0) {Hint: Type the symbol used to assign a default value to b.
function power(base, exp = _) {Hint: Type the default value used for exp.
function counter(step _ 1) {Hint: Type the symbol used to assign a default value to step.
function scale(factor = _) {Hint: Type the default value used for factor.
Spot the error
Question 1
Afunction add(a, b == 0) {Bconsole.log(a + b);C}
Question 2
Afunction mul(a, b: 1) {Bconsole.log(a * b);C}
Question 3
Aconsole.log("hi");Bfunction greet(n "Al") {Cconsole.log(n);
Question 4
Aconsole.log("go");Bfunction power(let x = 2) {Cconsole.log(x);
Question 5
Alet a = 1;Bconsole.log(a);CFunction timer(s = 30) {Dconsole.log(s);
