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