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