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