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