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