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