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