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