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