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