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 triple(n) {
return n * 3;
}
show(triple(21)); -
2.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(27)); - 3.🧰 In function tip(bill), what is bill? (a) a loop (b) a screen (c) a parameter (d) an event
-
4.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(1) + points(4)); -
5.🧰 What does this code show?
function greet(name) {
return "Well done, " + name + "!";
}
show(greet("Zoya")); (a) greet (b) Well done, Zoya! (c) Zoya (d) Well done, name! -
6.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(85, 5)); -
7.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(4) + points(4)); -
8.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(10)); -
9.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(2) + points(5)); -
10.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(60, 3)); - 11.🧰 How do you run (call) a function called greet? (a) greet() (b) greet[] (c) run greet (d) #greet
-
12.🧰 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(1)); -
13.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(65, 2)); -
14.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(30, 4)); - 15.🧰 What is a function? (a) a named block of code (b) a screen size (c) a type of phone (d) a list of colours
-
16.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(25, 6)); -
17.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(5) + points(0)); -
18.🧰 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(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 50;
}
show(fee(3) + fee(7)); -
20.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(30)); -
21.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(75, 3)); -
22.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(75, 2)); -
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(5) + fee(9)); -
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(9) + fee(7)); -
25.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(65, 4)); -
26.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(5) + points(5)); -
27.🧰 What does this code show?
function greet(name) {
return "Hi, " + name + "!";
}
show(greet("Riya")); (a) Hi, Riya! (b) Hi, name! (c) Riya (d) greet -
28.🧰 What does this code show?
function greet(name) {
return "Hello, " + name + "!";
}
show(greet("Arjun")); (a) Hello, name! (b) greet (c) Hello, Arjun! (d) Arjun -
29.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(2) + points(0)); -
30.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(10, 3)); -
31.🧰 What does this code show?
function greet(name) {
return "Hello, " + name + "!";
}
show(greet("Aman")); (a) greet (b) Hello, name! (c) Hello, Aman! (d) Aman -
32.🧰 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(2) + fee(11)); -
33.🧰 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(3) + fee(8)); -
34.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(60, 2)); -
35.🧰 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(9)); -
36.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(35, 6)); -
37.🧰 What does this code show?
function greet(name) {
return "Welcome, " + name + "!";
}
show(greet("Zoya")); (a) Welcome, Zoya! (b) Zoya (c) greet (d) Welcome, name! -
38.🧰 What does this code show?
function greet(name) {
return "Hello, " + name + "!";
}
show(greet("Meera")); (a) Hello, name! (b) Meera (c) Hello, Meera! (d) greet -
39.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(17)); - 40.🧰 Why write a function? (a) to slow the app (b) to make code longer (c) to reuse code (d) to hide errors
Answer key
- 63
- 81
- a parameter
- 60
- Well done, Zoya!
- 425
- 90
- 30
- 80
- 180
- greet()
- 60
- 130
- 120
- a named block of code
- 150
- 60
- 40
- 50
- 60
- 225
- 150
- 60
- 120
- 260
- 110
- Hi, Riya!
- Hello, Arjun!
- 30
- 30
- Hello, Aman!
- 50
- 40
- 120
- 20
- 210
- Welcome, Zoya!
- Hello, Meera!
- 34
- to reuse code
Free app development lessons, practice and worksheets at talentjr.in/app-development-for-students