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