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