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