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