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