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