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