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