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