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