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 reuse code (b) to hide errors (c) to make code longer (d) to slow the app
-
2.🧰 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(10) + fee(9)); -
3.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(20, 2)); -
4.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(45, 6)); -
5.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(3) + points(0)); -
6.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(35, 4)); -
7.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(5)); -
8.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(12)); -
9.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(35, 2)); -
10.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(5) + points(4)); -
11.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(8)); -
12.🧰 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(8) + fee(9)); - 13.🧰 In function tip(bill), what is bill? (a) a loop (b) an event (c) a screen (d) a parameter
- 14.🧰 What does return do in a function? (a) closes the screen (b) deletes the app (c) repeats the code (d) sends back a value
- 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 points(stars) {
return stars * 10 + 5;
}
show(points(3) + points(4)); -
17.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(10)); -
18.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(90, 3)); -
19.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(28)); -
20.🧰 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(10) + fee(8)); -
21.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(2) + points(2)); -
22.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(23)); -
23.🧰 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(3)); -
24.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(2) + points(3)); -
25.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(5) + points(3)); -
26.🧰 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(1)); -
27.🧰 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(12) + fee(5)); -
28.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(30, 4)); -
29.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(60, 3)); -
30.🧰 What does this code show?
function greet(name) {
return "Hello, " + name + "!";
}
show(greet("Riya")); (a) Hello, Riya! (b) Hello, name! (c) Riya (d) greet -
31.🧰 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)); -
32.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(95, 5)); -
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(9) + fee(5)); -
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(5) + fee(3)); -
35.🧰 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)); -
36.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(24)); -
37.🧰 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(3)); -
38.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(1) + points(2)); -
39.🧰 What does this code show?
function greet(name) {
return "Welcome, " + name + "!";
}
show(greet("Meera")); (a) greet (b) Meera (c) Welcome, name! (d) Welcome, Meera! -
40.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(29));
Answer key
- to reuse code
- 120
- 40
- 270
- 40
- 140
- 15
- 36
- 70
- 100
- 16
- 120
- a parameter
- sends back a value
- a named block of code
- 80
- 30
- 270
- 84
- 80
- 50
- 69
- 20
- 60
- 90
- 20
- 100
- 120
- 180
- Hello, Riya!
- 60
- 475
- 100
- 20
- 40
- 48
- 20
- 40
- Welcome, Meera!
- 87
Free app development lessons, practice and worksheets at talentjr.in/app-development-for-students