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