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