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