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