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