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