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