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