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