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 total(price, qty) {
return price * qty;
}
show(total(85, 2)); -
2.🧰 What does this code show?
function greet(name) {
return "Well done, " + name + "!";
}
show(greet("Kabir")); (a) greet (b) Kabir (c) Well done, name! (d) Well done, Kabir! -
3.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(5)); -
4.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(25)); - 5.🧰 What is a function? (a) a list of colours (b) a screen size (c) a named block of code (d) a type of phone
-
6.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(1) + points(4)); -
7.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(5) + points(0)); - 8.🧰 What does return do in a function? (a) sends back a value (b) deletes the app (c) closes the screen (d) repeats the code
-
9.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(2) + points(4)); -
10.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(32)); -
11.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(5) + points(3)); - 12.🧰 In function tip(bill), what is bill? (a) a parameter (b) a loop (c) an event (d) a screen
-
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 total(price, qty) {
return price * qty;
}
show(total(75, 2)); -
15.🧰 What does this code show?
function greet(name) {
return "Hi, " + name + "!";
}
show(greet("Riya")); (a) Hi, Riya! (b) greet (c) Riya (d) Hi, name! -
16.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(4) + points(3)); -
17.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(39)); -
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(4) + fee(5)); -
19.🧰 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(5) + fee(12)); -
20.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(70, 4)); -
21.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(35, 6)); -
22.🧰 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(4)); -
23.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(27)); -
24.🧰 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(5)); -
25.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(1) + points(1)); -
26.🧰 What does this code show?
function greet(name) {
return "Hello, " + name + "!";
}
show(greet("Riya")); (a) Hello, name! (b) greet (c) Hello, Riya! (d) Riya -
27.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(20, 4)); -
28.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(2) + points(0)); -
29.🧰 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)); -
30.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(25, 4)); -
31.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(40, 5)); -
32.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(45, 3)); -
33.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(30)); - 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 points(stars) {
return stars * 10 + 5;
}
show(points(5) + points(1)); -
36.🧰 What does this code show?
function greet(name) {
return "Hello, " + name + "!";
}
show(greet("Meera")); (a) greet (b) Hello, name! (c) Meera (d) Hello, Meera! -
37.🧰 What does this code show?
function greet(name) {
return "Hi, " + name + "!";
}
show(greet("Kabir")); (a) Hi, Kabir! (b) greet (c) Kabir (d) Hi, name! -
38.🧰 What does this code show?
function greet(name) {
return "Hi, " + name + "!";
}
show(greet("Aman")); (a) Hi, Aman! (b) Aman (c) greet (d) Hi, name! -
39.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(80, 6)); -
40.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(12));
Answer key
- 170
- Well done, Kabir!
- 15
- 75
- a named block of code
- 60
- 60
- sends back a value
- 70
- 64
- 90
- a parameter
- 40
- 150
- Hi, Riya!
- 80
- 117
- 60
- 80
- 280
- 210
- 60
- 81
- 20
- 30
- Hello, Riya!
- 80
- 30
- 50
- 100
- 200
- 135
- 90
- to reuse code
- 70
- Hello, Meera!
- Hi, Kabir!
- Hi, Aman!
- 480
- 24
Free app development lessons, practice and worksheets at talentjr.in/app-development-for-students