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(11) + fee(3)); -
2.🧰 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(8)); - 3.🧰 What does return do in a function? (a) sends back a value (b) deletes the app (c) repeats the code (d) closes the screen
-
4.🧰 What does this code show?
function greet(name) {
return "Hello, " + name + "!";
}
show(greet("Riya")); (a) Hello, name! (b) Hello, Riya! (c) Riya (d) greet -
5.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(1) + points(0)); -
6.🧰 What does this code show?
function greet(name) {
return "Hello, " + name + "!";
}
show(greet("Kabir")); (a) Hello, Kabir! (b) greet (c) Kabir (d) Hello, name! -
7.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(40, 3)); -
8.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(29)); -
9.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(8)); -
10.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(2) + points(5)); -
11.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(5) + points(2)); -
12.🧰 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(10)); -
13.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(17)); -
14.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(2) + points(1)); -
15.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(3) + points(0)); -
16.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(45, 2)); - 17.🧰 Why write a function? (a) to reuse code (b) to hide errors (c) to make code longer (d) to slow the app
-
18.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(38)); -
19.🧰 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! - 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 30;
}
show(fee(9) + fee(6)); -
22.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(3) + points(3)); -
23.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(28)); -
24.🧰 What does this code show?
function greet(name) {
return "Well done, " + name + "!";
}
show(greet("Aman")); (a) Aman (b) greet (c) Well done, Aman! (d) Well done, name! -
25.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(95, 6)); -
26.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(3) + points(5)); -
27.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(6)); -
28.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(10)); -
29.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(23)); -
30.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(23)); -
31.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(4)); -
32.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(34)); -
33.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(5) + points(0)); -
34.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(20, 3)); -
35.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(22)); -
36.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(90, 3)); -
37.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(5) + points(1)); -
38.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(3)); -
39.🧰 What does this code show?
function greet(name) {
return "Hi, " + name + "!";
}
show(greet("Riya")); (a) Riya (b) Hi, name! (c) greet (d) Hi, Riya! -
40.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(85, 3));
Answer key
- 60
- 40
- sends back a value
- Hello, Riya!
- 20
- Hello, Kabir!
- 120
- 58
- 24
- 80
- 80
- 100
- 51
- 40
- 40
- 90
- to reuse code
- 114
- Well done, Riya!
- a parameter
- 60
- 70
- 84
- Well done, Aman!
- 570
- 90
- 12
- 30
- 69
- 46
- 8
- 102
- 60
- 60
- 44
- 270
- 70
- 6
- Hi, Riya!
- 255
Free app development lessons, practice and worksheets at talentjr.in/app-development-for-students