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(13)); -
2.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(5) + points(4)); -
3.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(25, 2)); -
4.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(45, 6)); -
5.🧰 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(7)); -
6.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(4) + points(0)); -
7.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(75, 5)); -
8.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(1) + points(2)); - 9.🧰 Why write a function? (a) to make code longer (b) to slow the app (c) to hide errors (d) to reuse code
-
10.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(30, 3)); -
11.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(15)); - 12.🧰 What does return do in a function? (a) repeats the code (b) sends back a value (c) closes the screen (d) deletes the app
-
13.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(39)); -
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(3) + fee(2)); -
15.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(90, 6)); -
16.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(5) + points(1)); -
17.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(100, 2)); -
18.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(5)); - 19.🧰 How do you run (call) a function called greet? (a) #greet (b) greet[] (c) run greet (d) greet()
-
20.🧰 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(2) + fee(4)); -
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(9) + fee(2)); -
22.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(45, 5)); -
23.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(85, 6)); -
24.🧰 What does this code show?
function greet(name) {
return "Hi, " + name + "!";
}
show(greet("Kabir")); (a) greet (b) Hi, Kabir! (c) Kabir (d) Hi, name! -
25.🧰 What does this code show?
function greet(name) {
return "Welcome, " + name + "!";
}
show(greet("Meera")); (a) Meera (b) greet (c) Welcome, Meera! (d) Welcome, name! -
26.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(2) + points(2)); -
27.🧰 What does this code show?
function greet(name) {
return "Well done, " + name + "!";
}
show(greet("Riya")); (a) Well done, name! (b) Riya (c) greet (d) Well done, Riya! - 28.🧰 In function tip(bill), what is bill? (a) a screen (b) a parameter (c) a loop (d) an event
-
29.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(60, 5)); -
30.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(33)); -
31.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(85, 5)); -
32.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(32)); -
33.🧰 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(4) + fee(11)); -
34.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(85, 4)); -
35.🧰 What does this code show?
function greet(name) {
return "Welcome, " + name + "!";
}
show(greet("Riya")); (a) greet (b) Welcome, name! (c) Riya (d) Welcome, Riya! -
36.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(65, 4)); -
37.🧰 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(1)); -
38.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(55, 6)); -
39.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(11)); -
40.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(60, 3));
Answer key
- 39
- 100
- 50
- 270
- 60
- 50
- 375
- 40
- to reuse code
- 90
- 45
- sends back a value
- 78
- 0
- 540
- 70
- 200
- 10
- greet()
- 0
- 40
- 225
- 510
- Hi, Kabir!
- Welcome, Meera!
- 50
- Well done, Riya!
- a parameter
- 300
- 99
- 425
- 96
- 30
- 340
- Welcome, Riya!
- 260
- 50
- 330
- 33
- 180
Free app development lessons, practice and worksheets at talentjr.in/app-development-for-students