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