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.🧰 In function tip(bill), what is bill? (a) a screen (b) a loop (c) an event (d) a parameter
-
2.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(15, 5)); -
3.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(75, 2)); -
4.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(2) + points(5)); -
5.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(23)); -
6.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(30, 4)); -
7.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(90, 4)); -
8.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(40, 6)); -
9.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(45, 5)); -
10.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(90, 6)); -
11.🧰 What does this code show?
function greet(name) {
return "Hello, " + name + "!";
}
show(greet("Aman")); (a) Aman (b) Hello, name! (c) Hello, Aman! (d) greet -
12.🧰 What does this code show?
function greet(name) {
return "Hello, " + name + "!";
}
show(greet("Meera")); (a) Hello, name! (b) Meera (c) greet (d) Hello, Meera! -
13.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(3)); -
14.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(40, 4)); -
15.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(10)); -
16.🧰 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(7) + fee(10)); -
17.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(35, 3)); -
18.🧰 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(10) + fee(2)); -
19.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(2) + points(0)); -
20.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(39)); -
21.🧰 What does this code show?
function greet(name) {
return "Hi, " + name + "!";
}
show(greet("Aman")); (a) Hi, name! (b) greet (c) Aman (d) Hi, Aman! - 22.🧰 Why write a function? (a) to make code longer (b) to slow the app (c) to hide errors (d) to reuse code
-
23.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(15, 3)); -
24.🧰 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(9) + fee(2)); - 25.🧰 What does return do in a function? (a) repeats the code (b) sends back a value (c) deletes the app (d) closes the screen
-
26.🧰 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(2)); -
27.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(45, 4)); -
28.🧰 What does this code show?
function greet(name) {
return "Well done, " + name + "!";
}
show(greet("Aman")); (a) Well done, name! (b) Well done, Aman! (c) Aman (d) greet -
29.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(23)); -
30.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(10, 6)); -
31.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(13)); -
32.🧰 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(7) + fee(3)); -
33.🧰 What does this code show?
function greet(name) {
return "Welcome, " + name + "!";
}
show(greet("Zoya")); (a) greet (b) Zoya (c) Welcome, name! (d) Welcome, Zoya! -
34.🧰 What does this code show?
function greet(name) {
return "Hi, " + name + "!";
}
show(greet("Kabir")); (a) greet (b) Hi, Kabir! (c) Kabir (d) Hi, name! -
35.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(30, 2)); -
36.🧰 What does this code show?
function greet(name) {
return "Hi, " + name + "!";
}
show(greet("Zoya")); (a) Hi, Zoya! (b) Zoya (c) Hi, name! (d) greet -
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(10) + fee(2)); -
38.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(2) + points(1)); -
39.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(4)); -
40.🧰 What does this code show?
function greet(name) {
return "Hi, " + name + "!";
}
show(greet("Riya")); (a) Hi, name! (b) greet (c) Hi, Riya! (d) Riya
Answer key
- a parameter
- 75
- 150
- 80
- 46
- 120
- 360
- 240
- 225
- 540
- Hello, Aman!
- Hello, Meera!
- 9
- 160
- 20
- 120
- 105
- 50
- 30
- 78
- Hi, Aman!
- to reuse code
- 45
- 20
- sends back a value
- 0
- 180
- Well done, Aman!
- 69
- 60
- 26
- 60
- Welcome, Zoya!
- Hi, Kabir!
- 60
- Hi, Zoya!
- 30
- 40
- 12
- Hi, Riya!
Free app development lessons, practice and worksheets at talentjr.in/app-development-for-students