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