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