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.🧰 Why write a function? (a) to slow the app (b) to hide errors (c) to reuse code (d) to make code longer
-
3.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(45, 3)); - 4.🧰 What does return do in a function? (a) sends back a value (b) repeats the code (c) closes the screen (d) deletes the app
-
5.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(4)); - 6.🧰 What is a function? (a) a list of colours (b) a named block of code (c) a screen size (d) a type of phone
-
7.🧰 What does this code show?
function greet(name) {
return "Hello, " + name + "!";
}
show(greet("Meera")); (a) greet (b) Hello, name! (c) Meera (d) Hello, Meera! -
8.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(30, 6)); -
9.🧰 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(12)); -
10.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(85, 5)); -
11.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(20, 5)); -
12.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(70, 3)); -
13.🧰 What does this code show?
function greet(name) {
return "Hi, " + name + "!";
}
show(greet("Aman")); (a) Aman (b) greet (c) Hi, name! (d) Hi, Aman! -
14.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(50, 2)); -
15.🧰 What does this code show?
function greet(name) {
return "Hello, " + name + "!";
}
show(greet("Kabir")); (a) Hello, Kabir! (b) greet (c) Kabir (d) Hello, name! -
16.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(2) + points(5)); -
17.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(2) + points(4)); -
18.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(17)); -
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(6) + fee(5)); -
20.🧰 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(6)); -
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(11) + fee(12)); -
22.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(2) + points(0)); -
23.🧰 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(1)); -
24.🧰 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(11) + fee(1)); -
25.🧰 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(12) + fee(3)); -
26.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(85, 4)); -
27.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(100, 3)); -
28.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(3) + points(3)); -
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 double(n) {
return n * 2;
}
show(double(38)); -
31.🧰 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(11) + fee(4)); -
32.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(39)); - 33.🧰 In function tip(bill), what is bill? (a) a parameter (b) a loop (c) a screen (d) an event
-
34.🧰 What does this code show?
function greet(name) {
return "Hello, " + name + "!";
}
show(greet("Zoya")); (a) Zoya (b) Hello, name! (c) greet (d) Hello, Zoya! -
35.🧰 What does this code show?
function greet(name) {
return "Hi, " + name + "!";
}
show(greet("Meera")); (a) Meera (b) greet (c) Hi, Meera! (d) Hi, name! -
36.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(16)); -
37.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(5) + points(3)); -
38.🧰 What does this code show?
function greet(name) {
return "Well done, " + name + "!";
}
show(greet("Meera")); (a) Well done, name! (b) greet (c) Well done, Meera! (d) Meera -
39.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(5) + points(0)); -
40.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(55, 6));
Answer key
- 70
- to reuse code
- 135
- sends back a value
- 12
- a named block of code
- Hello, Meera!
- 180
- 120
- 425
- 100
- 210
- Hi, Aman!
- 100
- Hello, Kabir!
- 80
- 70
- 34
- 80
- 100
- 60
- 30
- 0
- 60
- 20
- 340
- 300
- 70
- 190
- 76
- 60
- 78
- a parameter
- Hello, Zoya!
- Hi, Meera!
- 32
- 90
- Well done, Meera!
- 60
- 330
Free app development lessons, practice and worksheets at talentjr.in/app-development-for-students