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