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, 3)); -
2.🧰 What does this code show?
function greet(name) {
return "Welcome, " + name + "!";
}
show(greet("Riya")); (a) greet (b) Riya (c) Welcome, Riya! (d) Welcome, name! -
3.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(25, 4)); -
4.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(1) + points(3)); - 5.🧰 What is a function? (a) a type of phone (b) a named block of code (c) a screen size (d) a list of colours
-
6.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(1) + points(5)); -
7.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(5)); -
8.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(40)); -
9.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(11)); -
10.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(29)); - 11.🧰 How do you run (call) a function called greet? (a) #greet (b) greet() (c) greet[] (d) run greet
-
12.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(45, 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(4) + fee(8)); -
14.🧰 What does this code show?
function greet(name) {
return "Hello, " + name + "!";
}
show(greet("Zoya")); (a) Hello, name! (b) greet (c) Zoya (d) Hello, Zoya! -
15.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(5) + points(0)); -
16.🧰 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(9) + fee(4)); -
17.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(10)); -
18.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(60, 3)); -
19.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(55, 6)); - 20.🧰 Why write a function? (a) to reuse code (b) to hide errors (c) to slow the app (d) to make code longer
-
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(1) + fee(8)); -
22.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(4) + points(4)); -
23.🧰 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(8) + fee(2)); -
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(3) + fee(10)); -
25.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(3)); -
26.🧰 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(1) + fee(12)); -
27.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(90, 3)); -
28.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(60, 4)); -
29.🧰 What does this code show?
function greet(name) {
return "Welcome, " + name + "!";
}
show(greet("Aman")); (a) Welcome, name! (b) Welcome, Aman! (c) Aman (d) greet -
30.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(1) + points(4)); -
31.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(5) + points(3)); -
32.🧰 What does this code show?
function greet(name) {
return "Hi, " + name + "!";
}
show(greet("Riya")); (a) Riya (b) Hi, name! (c) greet (d) Hi, Riya! -
33.🧰 What does this code show?
function greet(name) {
return "Hi, " + name + "!";
}
show(greet("Aman")); (a) Hi, Aman! (b) Aman (c) Hi, name! (d) greet -
34.🧰 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(10) + fee(6)); -
35.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(3) + points(0)); -
36.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(15, 4)); -
37.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(26)); -
38.🧰 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(10)); -
39.🧰 What does this code show?
function greet(name) {
return "Well done, " + name + "!";
}
show(greet("Kabir")); (a) Kabir (b) Well done, name! (c) greet (d) Well done, Kabir! -
40.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(1) + points(1));
Answer key
- 255
- Welcome, Riya!
- 100
- 50
- a named block of code
- 70
- 10
- 120
- 22
- 58
- greet()
- 180
- 20
- Hello, Zoya!
- 60
- 20
- 30
- 180
- 330
- to reuse code
- 30
- 90
- 30
- 60
- 9
- 60
- 270
- 240
- Welcome, Aman!
- 60
- 90
- Hi, Riya!
- Hi, Aman!
- 120
- 40
- 60
- 52
- 40
- Well done, Kabir!
- 30
Free app development lessons, practice and worksheets at talentjr.in/app-development-for-students