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(4)); -
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(3) + fee(11)); -
3.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(4) + points(3)); -
4.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(100, 3)); -
5.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(70, 5)); -
6.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(55, 4)); - 7.🧰 In function tip(bill), what is bill? (a) a parameter (b) a screen (c) a loop (d) an event
-
8.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(29)); -
9.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(50, 2)); -
10.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(70, 2)); -
11.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(1) + points(2)); - 12.🧰 What is a function? (a) a list of colours (b) a screen size (c) a type of phone (d) a named block of code
-
13.🧰 What does this code show?
function greet(name) {
return "Hello, " + name + "!";
}
show(greet("Zoya")); (a) Hello, name! (b) greet (c) Hello, Zoya! (d) Zoya -
14.🧰 What does this code show?
function greet(name) {
return "Well done, " + name + "!";
}
show(greet("Aman")); (a) Well done, name! (b) greet (c) Aman (d) Well done, Aman! - 15.🧰 What does return do in a function? (a) closes the screen (b) repeats the code (c) deletes the app (d) sends back a value
-
16.🧰 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(4) + fee(2)); -
17.🧰 What does this code show?
function greet(name) {
return "Hello, " + name + "!";
}
show(greet("Meera")); (a) greet (b) Hello, Meera! (c) Meera (d) Hello, name! -
18.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(50, 6)); -
19.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(19)); -
20.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(5) + points(5)); -
21.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(38)); -
22.🧰 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(11) + fee(12)); -
23.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(65, 2)); -
24.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(4)); -
25.🧰 A museum app works out the entry fee. What number does this code show?
function fee(age) {
if (age < 5) {
return 0;
}
return 30;
}
show(fee(6) + fee(6)); -
26.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(31)); -
27.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(3) + points(1)); -
28.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(1) + points(3)); -
29.🧰 What does this code show?
function greet(name) {
return "Hi, " + name + "!";
}
show(greet("Meera")); (a) Meera (b) Hi, name! (c) greet (d) Hi, Meera! -
30.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(80, 3)); -
31.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(35)); -
32.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(26)); -
33.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(100, 6)); -
34.🧰 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(10) + fee(12)); -
35.🧰 A museum app works out the entry fee. What number does this code show?
function fee(age) {
if (age < 5) {
return 0;
}
return 50;
}
show(fee(4) + fee(4)); -
36.🧰 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(9) + fee(12)); -
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 triple(n) {
return n * 3;
}
show(triple(34)); -
39.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(3) + points(0)); -
40.🧰 What does this code show?
function greet(name) {
return "Hi, " + name + "!";
}
show(greet("Aman")); (a) Hi, Aman! (b) greet (c) Hi, name! (d) Aman
Answer key
- 12
- 60
- 80
- 300
- 350
- 220
- a parameter
- 58
- 100
- 140
- 40
- a named block of code
- Hello, Zoya!
- Well done, Aman!
- sends back a value
- 0
- Hello, Meera!
- 300
- 38
- 110
- 76
- 80
- 130
- 8
- 60
- 62
- 50
- 50
- Hi, Meera!
- 240
- 70
- 78
- 600
- 120
- 0
- 40
- 40
- 102
- 40
- Hi, Aman!
Free app development lessons, practice and worksheets at talentjr.in/app-development-for-students