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