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