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