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 total(price, qty) {
return price * qty;
}
show(total(65, 4)); -
2.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(31)); -
3.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(30)); -
4.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(10, 5)); -
5.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(40)); -
6.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(95, 5)); -
7.🧰 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(7)); -
8.🧰 What does this code show?
function greet(name) {
return "Well done, " + name + "!";
}
show(greet("Aman")); (a) Well done, name! (b) Well done, Aman! (c) Aman (d) greet -
9.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(24)); -
10.🧰 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(8) + fee(1)); -
11.🧰 What does this code show?
function greet(name) {
return "Hi, " + name + "!";
}
show(greet("Riya")); (a) Hi, name! (b) greet (c) Riya (d) Hi, Riya! -
12.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(16)); -
13.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(85, 6)); -
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(11) + fee(12)); -
15.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(80, 6)); -
16.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(5)); - 17.🧰 In function tip(bill), what is bill? (a) a screen (b) a loop (c) a parameter (d) an event
-
18.🧰 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(1) + fee(7)); -
19.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(10)); -
20.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(2) + points(2)); -
21.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(5) + points(2)); - 22.🧰 How do you run (call) a function called greet? (a) greet[] (b) #greet (c) greet() (d) run greet
-
23.🧰 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(8) + fee(6)); -
24.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(4) + points(1)); -
25.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(5) + points(1)); -
26.🧰 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(11) + fee(1)); -
27.🧰 What does this code show?
function greet(name) {
return "Welcome, " + name + "!";
}
show(greet("Riya")); (a) Riya (b) Welcome, name! (c) greet (d) Welcome, Riya! -
28.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(10, 4)); -
29.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(5) + points(4)); -
30.🧰 What does this code show?
function greet(name) {
return "Hi, " + name + "!";
}
show(greet("Aman")); (a) Aman (b) greet (c) Hi, name! (d) Hi, Aman! -
31.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(85, 3)); -
32.🧰 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(8) + fee(12)); -
33.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(3) + points(0)); -
34.🧰 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(7) + fee(9)); -
35.🧰 What does this code show?
function greet(name) {
return "Welcome, " + name + "!";
}
show(greet("Zoya")); (a) Zoya (b) Welcome, name! (c) Welcome, Zoya! (d) greet - 36.🧰 What is a function? (a) a type of phone (b) a list of colours (c) a named block of code (d) a screen size
-
37.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(2) + points(0)); - 38.🧰 Why write a function? (a) to reuse code (b) to hide errors (c) to make code longer (d) to slow the app
-
39.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(28)); -
40.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(9));
Answer key
- 260
- 62
- 90
- 50
- 120
- 475
- 40
- Well done, Aman!
- 48
- 40
- Hi, Riya!
- 48
- 510
- 40
- 480
- 10
- a parameter
- 20
- 30
- 50
- 80
- greet()
- 40
- 60
- 70
- 20
- Welcome, Riya!
- 40
- 100
- Hi, Aman!
- 255
- 40
- 40
- 80
- Welcome, Zoya!
- a named block of code
- 30
- to reuse code
- 84
- 27
Free app development lessons, practice and worksheets at talentjr.in/app-development-for-students