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