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