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