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