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