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