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