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