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