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(12) + fee(3)); -
2.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(25, 6)); -
3.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(85, 5)); -
4.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(100, 3)); - 5.🧰 In function tip(bill), what is bill? (a) an event (b) a parameter (c) a loop (d) a screen
-
6.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(2) + points(2)); -
7.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(3) + points(1)); -
8.🧰 What does this code show?
function greet(name) {
return "Hello, " + name + "!";
}
show(greet("Riya")); (a) Riya (b) Hello, Riya! (c) Hello, name! (d) greet -
9.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(90, 5)); -
10.🧰 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(4) + fee(6)); -
11.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(2) + points(1)); -
12.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(34)); -
13.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(80, 2)); -
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(12) + fee(2)); -
15.🧰 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(8)); -
16.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(3) + points(3)); -
17.🧰 What does this code show?
function greet(name) {
return "Hi, " + name + "!";
}
show(greet("Aman")); (a) Hi, Aman! (b) Aman (c) greet (d) Hi, name! -
18.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(2) + points(5)); -
19.🧰 What does this code show?
function greet(name) {
return "Hello, " + name + "!";
}
show(greet("Zoya")); (a) Zoya (b) Hello, Zoya! (c) greet (d) Hello, name! - 20.🧰 What is a function? (a) a list of colours (b) a screen size (c) a type of phone (d) a named block of code
-
21.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(15, 6)); -
22.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(4) + points(4)); -
23.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(70, 5)); - 24.🧰 Why write a function? (a) to reuse code (b) to make code longer (c) to slow the app (d) to hide errors
-
25.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(95, 6)); -
26.🧰 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(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 total(price, qty) {
return price * qty;
}
show(total(20, 3)); -
29.🧰 What does this code show?
function greet(name) {
return "Welcome, " + name + "!";
}
show(greet("Riya")); (a) Welcome, Riya! (b) greet (c) Welcome, name! (d) Riya -
30.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(95, 3)); -
31.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(85, 2)); -
32.🧰 What does this code show?
function greet(name) {
return "Hi, " + name + "!";
}
show(greet("Kabir")); (a) Kabir (b) greet (c) Hi, name! (d) Hi, Kabir! - 33.🧰 What does return do in a function? (a) repeats the code (b) deletes the app (c) sends back a value (d) closes the screen
-
34.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(15, 3)); -
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(11) + fee(2)); -
36.🧰 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(2)); -
37.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(32)); -
38.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(33)); -
39.🧰 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(10) + fee(1)); -
40.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(29));
Answer key
- 40
- 150
- 425
- 300
- a parameter
- 50
- 50
- Hello, Riya!
- 450
- 20
- 40
- 68
- 160
- 40
- 40
- 70
- Hi, Aman!
- 80
- Hello, Zoya!
- a named block of code
- 90
- 90
- 350
- to reuse code
- 570
- 0
- greet()
- 60
- Welcome, Riya!
- 285
- 170
- Hi, Kabir!
- sends back a value
- 45
- 30
- 0
- 96
- 66
- 20
- 87
Free app development lessons, practice and worksheets at talentjr.in/app-development-for-students