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