App Dev Worksheet: Functions
Developer's tip (Write once, use often): function name(parameter) { …; return result; } — call it with name(value). Work out each call separately, then finish the line. Inside a function, if / return can give different results for different inputs.
NameDateTime takenScore ___ / 40
-
1.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(15)); -
2.🧰 A museum app works out the entry fee. What number does this code show?
function fee(age) {
if (age < 5) {
return 0;
}
return 60;
}
show(fee(5) + fee(8)); - 3.🧰 Why write a function? (a) to hide errors (b) to slow the app (c) to make code longer (d) to reuse code
-
4.🧰 A museum app works out the entry fee. What number does this code show?
function fee(age) {
if (age < 5) {
return 0;
}
return 60;
}
show(fee(12) + fee(6)); -
5.🧰 A museum app works out the entry fee. What number does this code show?
function fee(age) {
if (age < 5) {
return 0;
}
return 20;
}
show(fee(2) + fee(5)); - 6.🧰 What is a function? (a) a named block of code (b) a type of phone (c) a screen size (d) a list of colours
-
7.🧰 What does this code show?
function greet(name) {
return "Hi, " + name + "!";
}
show(greet("Kabir")); (a) Hi, name! (b) greet (c) Hi, Kabir! (d) Kabir - 8.🧰 In function tip(bill), what is bill? (a) an event (b) a parameter (c) a screen (d) a loop
- 9.🧰 How do you run (call) a function called greet? (a) run greet (b) #greet (c) greet() (d) greet[]
-
10.🧰 A museum app works out the entry fee. What number does this code show?
function fee(age) {
if (age < 5) {
return 0;
}
return 20;
}
show(fee(3) + fee(6)); -
11.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(39)); -
12.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(3) + points(2)); -
13.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(2) + points(2)); -
14.🧰 A museum app works out the entry fee. What number does this code show?
function fee(age) {
if (age < 5) {
return 0;
}
return 20;
}
show(fee(2) + fee(11)); -
15.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(33)); -
16.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(24)); -
17.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(1) + points(5)); -
18.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(12)); -
19.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(4) + points(2)); -
20.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(35, 2)); -
21.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(26)); -
22.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(95, 5)); -
23.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(20, 3)); -
24.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(1) + points(4)); -
25.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(45, 5)); -
26.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(22)); -
27.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(5) + points(4)); -
28.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(85, 4)); -
29.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(40, 5)); -
30.🧰 A museum app works out the entry fee. What number does this code show?
function fee(age) {
if (age < 5) {
return 0;
}
return 40;
}
show(fee(1) + fee(6)); -
31.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(22)); -
32.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(10)); -
33.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(3)); -
34.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(55, 2)); -
35.🧰 What does this code show?
function greet(name) {
return "Well done, " + name + "!";
}
show(greet("Meera")); (a) Well done, Meera! (b) greet (c) Well done, name! (d) Meera -
36.🧰 What does this code show?
function greet(name) {
return "Hi, " + name + "!";
}
show(greet("Arjun")); (a) greet (b) Arjun (c) Hi, Arjun! (d) Hi, name! -
37.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(2) + points(1)); -
38.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(4) + points(1)); -
39.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(14)); -
40.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(19));
Answer key
- 45
- 120
- to reuse code
- 120
- 20
- a named block of code
- Hi, Kabir!
- a parameter
- greet()
- 20
- 117
- 60
- 50
- 20
- 99
- 48
- 70
- 36
- 70
- 70
- 78
- 475
- 60
- 60
- 225
- 44
- 100
- 340
- 200
- 40
- 66
- 20
- 6
- 110
- Well done, Meera!
- Hi, Arjun!
- 40
- 60
- 28
- 57
Free app development lessons, practice and worksheets at talentjr.in/app-development-for-students