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