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