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