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