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 20;
}
show(fee(5) + fee(7)); -
2.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(15, 4)); -
3.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(22)); -
4.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(25)); -
5.🧰 What does this code show?
function greet(name) {
return "Hello, " + name + "!";
}
show(greet("Aman")); (a) Aman (b) greet (c) Hello, Aman! (d) Hello, name! -
6.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(1) + points(3)); -
7.🧰 What does this code show?
function greet(name) {
return "Welcome, " + name + "!";
}
show(greet("Meera")); (a) Welcome, Meera! (b) Welcome, name! (c) Meera (d) greet -
8.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(26)); -
9.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(13)); -
10.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(45, 5)); -
11.🧰 What does this code show?
function greet(name) {
return "Well done, " + name + "!";
}
show(greet("Kabir")); (a) Well done, name! (b) greet (c) Kabir (d) Well done, Kabir! -
12.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(15, 3)); -
13.🧰 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(4)); -
14.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(30, 2)); -
15.🧰 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(7)); - 16.🧰 What does return do in a function? (a) repeats the code (b) sends back a value (c) closes the screen (d) deletes the app
-
17.🧰 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(3) + fee(8)); -
18.🧰 What does this code show?
function greet(name) {
return "Welcome, " + name + "!";
}
show(greet("Arjun")); (a) Arjun (b) greet (c) Welcome, name! (d) Welcome, Arjun! -
19.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(4)); - 20.🧰 What is a function? (a) a named block of code (b) a type of phone (c) a screen size (d) a list of colours
- 21.🧰 Why write a function? (a) to make code longer (b) to hide errors (c) to reuse code (d) to slow the app
-
22.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(21)); -
23.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(95, 5)); -
24.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(2) + points(3)); -
25.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(33)); - 26.🧰 How do you run (call) a function called greet? (a) run greet (b) #greet (c) greet[] (d) greet()
-
27.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(35)); -
28.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(20, 3)); -
29.🧰 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! -
30.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(10)); -
31.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(35, 6)); -
32.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(60, 3)); -
33.🧰 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(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 "Hi, " + name + "!";
}
show(greet("Aman")); (a) greet (b) Aman (c) Hi, name! (d) Hi, Aman! -
36.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(10, 2)); -
37.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(20, 4)); -
38.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(27)); -
39.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(3) + points(5)); -
40.🧰 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(7));
Answer key
- 40
- 60
- 44
- 75
- Hello, Aman!
- 50
- Welcome, Meera!
- 78
- 39
- 225
- Well done, Kabir!
- 45
- 0
- 60
- 100
- sends back a value
- 40
- Welcome, Arjun!
- 8
- a named block of code
- to reuse code
- 42
- 475
- 60
- 66
- greet()
- 105
- 60
- Well done, Zoya!
- 30
- 210
- 180
- 0
- 110
- Hi, Aman!
- 20
- 80
- 81
- 90
- 40
Free app development lessons, practice and worksheets at talentjr.in/app-development-for-students