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