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