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