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