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