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