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