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