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