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