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