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(12)); - 2.🧰 What does return do in a function? (a) closes the screen (b) deletes the app (c) repeats the code (d) sends back a value
-
3.🧰 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)); -
4.🧰 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(8)); -
5.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(35, 3)); -
6.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(4) + points(2)); -
7.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(80, 3)); -
8.🧰 What does this code show?
function greet(name) {
return "Welcome, " + name + "!";
}
show(greet("Kabir")); (a) greet (b) Welcome, name! (c) Kabir (d) Welcome, Kabir! -
9.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(95, 6)); -
10.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(32)); -
11.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(32)); -
12.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(5) + points(1)); -
13.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(5) + points(5)); -
14.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(33)); -
15.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(3)); -
16.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(17)); -
17.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(15, 2)); -
18.🧰 What does this code show?
function greet(name) {
return "Hi, " + name + "!";
}
show(greet("Kabir")); (a) Hi, name! (b) greet (c) Hi, Kabir! (d) Kabir -
19.🧰 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(10)); -
20.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(1) + points(1)); -
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(2) + fee(3)); -
22.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(15)); - 23.🧰 How do you run (call) a function called greet? (a) #greet (b) greet() (c) run greet (d) greet[]
-
24.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(75, 3)); -
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(1) + fee(12)); -
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(7) + fee(4)); -
27.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(75, 6)); -
28.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(37)); -
29.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(1) + points(4)); -
30.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(65, 2)); - 31.🧰 Why write a function? (a) to make code longer (b) to hide errors (c) to reuse code (d) to slow the app
-
32.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(28)); -
33.🧰 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(3) + fee(10)); -
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(8) + fee(5)); -
35.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(20, 4)); - 36.🧰 In function tip(bill), what is bill? (a) a screen (b) a loop (c) an event (d) a parameter
-
37.🧰 What does this code show?
function greet(name) {
return "Welcome, " + name + "!";
}
show(greet("Arjun")); (a) Welcome, name! (b) Arjun (c) Welcome, Arjun! (d) greet -
38.🧰 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(10)); -
39.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(50, 6)); -
40.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(3) + points(0));
Answer key
- 24
- sends back a value
- 20
- 60
- 105
- 70
- 240
- Welcome, Kabir!
- 570
- 96
- 64
- 70
- 110
- 99
- 6
- 51
- 30
- Hi, Kabir!
- 120
- 30
- 0
- 30
- greet()
- 225
- 20
- 50
- 450
- 111
- 60
- 130
- to reuse code
- 84
- 50
- 60
- 80
- a parameter
- Welcome, Arjun!
- 60
- 300
- 40
Free app development lessons, practice and worksheets at talentjr.in/app-development-for-students