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 60;
}
show(fee(1) + fee(8)); -
2.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(35, 5)); -
3.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(3)); -
4.🧰 What does this code show?
function greet(name) {
return "Hello, " + name + "!";
}
show(greet("Kabir")); (a) Hello, Kabir! (b) Kabir (c) greet (d) Hello, name! -
5.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(4) + points(4)); - 6.🧰 What is a function? (a) a named block of code (b) a screen size (c) a list of colours (d) a type of phone
- 7.🧰 Why write a function? (a) to hide errors (b) to slow the app (c) to reuse code (d) to make code longer
-
8.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(30, 4)); -
9.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(45, 6)); -
10.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(23)); -
11.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(8)); -
12.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(5) + points(1)); -
13.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(8)); -
14.🧰 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(3) + fee(1)); -
15.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(75, 2)); -
16.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(21)); -
17.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(27)); -
18.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(3) + points(5)); - 19.🧰 What does return do in a function? (a) repeats the code (b) sends back a value (c) closes the screen (d) deletes the app
- 20.🧰 In function tip(bill), what is bill? (a) an event (b) a loop (c) a parameter (d) a screen
-
21.🧰 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(1) + fee(6)); -
22.🧰 What does this code show?
function greet(name) {
return "Welcome, " + name + "!";
}
show(greet("Aman")); (a) greet (b) Welcome, Aman! (c) Aman (d) Welcome, name! -
23.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(55, 2)); -
24.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(25, 5)); -
25.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(31)); -
26.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(35)); -
27.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(90, 6)); -
28.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(15)); -
29.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(95, 4)); -
30.🧰 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(9) + fee(11)); -
31.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(4) + points(1)); -
32.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(36)); -
33.🧰 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(2)); -
34.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(35, 4)); -
35.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(85, 2)); -
36.🧰 What does this code show?
function greet(name) {
return "Well done, " + name + "!";
}
show(greet("Zoya")); (a) Well done, Zoya! (b) Zoya (c) Well done, name! (d) greet -
37.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(1) + points(1)); -
38.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(2) + points(1)); -
39.🧰 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(3)); -
40.🧰 What does this code show?
function greet(name) {
return "Hi, " + name + "!";
}
show(greet("Aman")); (a) Hi, name! (b) Hi, Aman! (c) Aman (d) greet
Answer key
- 60
- 175
- 6
- Hello, Kabir!
- 90
- a named block of code
- to reuse code
- 120
- 270
- 46
- 24
- 70
- 16
- 0
- 150
- 63
- 81
- 90
- sends back a value
- a parameter
- 60
- Welcome, Aman!
- 110
- 125
- 62
- 70
- 540
- 45
- 380
- 80
- 60
- 108
- 20
- 140
- 170
- Well done, Zoya!
- 30
- 40
- 50
- Hi, Aman!
Free app development lessons, practice and worksheets at talentjr.in/app-development-for-students