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