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