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