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