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