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