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