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