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