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