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