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