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