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