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 triple(n) {
return n * 3;
}
show(triple(22)); - 2.🧰 What is a function? (a) a named block of code (b) a list of colours (c) a screen size (d) a type of phone
-
3.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(30, 4)); -
4.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(38)); -
5.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(30, 3)); -
6.🧰 What does this code show?
function greet(name) {
return "Hi, " + name + "!";
}
show(greet("Aman")); (a) Aman (b) Hi, Aman! (c) Hi, name! (d) greet -
7.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(3) + points(2)); -
8.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(100, 3)); -
9.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(5) + points(4)); -
10.🧰 What does this code show?
function greet(name) {
return "Hello, " + name + "!";
}
show(greet("Riya")); (a) Hello, Riya! (b) greet (c) Hello, name! (d) Riya -
11.🧰 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(8)); -
12.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(5) + points(5)); -
13.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(45, 2)); -
14.🧰 What does this code show?
function greet(name) {
return "Hello, " + name + "!";
}
show(greet("Kabir")); (a) Kabir (b) Hello, name! (c) Hello, Kabir! (d) greet -
15.🧰 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(11) + fee(4)); -
16.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(21)); -
17.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(29)); -
18.🧰 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(5)); -
19.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(32)); -
20.🧰 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(8) + fee(2)); -
21.🧰 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(8) + fee(6)); -
22.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(40, 3)); -
23.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(65, 4)); -
24.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(25, 3)); - 25.🧰 Why write a function? (a) to slow the app (b) to reuse code (c) to make code longer (d) to hide errors
-
26.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(4) + points(1)); -
27.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(35, 3)); - 28.🧰 In function tip(bill), what is bill? (a) an event (b) a screen (c) a loop (d) a parameter
-
29.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(7)); -
30.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(55, 6)); -
31.🧰 What does this code show?
function greet(name) {
return "Hi, " + name + "!";
}
show(greet("Arjun")); (a) Arjun (b) Hi, Arjun! (c) Hi, name! (d) greet -
32.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(8)); -
33.🧰 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(1) + fee(8)); -
34.🧰 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(9)); -
35.🧰 What does this code show?
function greet(name) {
return "Welcome, " + name + "!";
}
show(greet("Kabir")); (a) Kabir (b) Welcome, name! (c) greet (d) Welcome, Kabir! -
36.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(2) + 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 30;
}
show(fee(9) + fee(3)); -
38.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(5)); -
39.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(3) + points(1)); -
40.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(2) + points(4));
Answer key
- 66
- a named block of code
- 120
- 114
- 90
- Hi, Aman!
- 60
- 300
- 100
- Hello, Riya!
- 20
- 110
- 90
- Hello, Kabir!
- 50
- 42
- 58
- 60
- 64
- 60
- 80
- 120
- 260
- 75
- to reuse code
- 60
- 105
- a parameter
- 14
- 330
- Hi, Arjun!
- 24
- 30
- 80
- Welcome, Kabir!
- 60
- 30
- 15
- 50
- 70
Free app development lessons, practice and worksheets at talentjr.in/app-development-for-students