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