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