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