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