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