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