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