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