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