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