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