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(5)); -
2.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(10, 2)); -
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(2) + fee(8)); -
4.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(50, 2)); -
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(7) + fee(11)); - 6.🧰 Why write a function? (a) to hide errors (b) to reuse code (c) to make code longer (d) to slow the app
-
7.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(5) + points(5)); -
8.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(2) + points(4)); -
9.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(5) + points(3)); -
10.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(5) + points(0)); -
11.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(3) + points(0)); -
12.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(80, 2)); -
13.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(5)); -
14.🧰 What does this code show?
function greet(name) {
return "Hello, " + name + "!";
}
show(greet("Aman")); (a) Hello, Aman! (b) Hello, name! (c) greet (d) Aman - 15.🧰 What does return do in a function? (a) sends back a value (b) deletes the app (c) repeats the code (d) closes the screen
-
16.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(1) + points(0)); -
17.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(11)); -
18.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(60, 2)); -
19.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(7)); -
20.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(70, 3)); -
21.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(85, 5)); -
22.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(50, 4)); -
23.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(40, 5)); -
24.🧰 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(3) + fee(4)); -
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(2) + fee(7)); -
26.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(5) + points(2)); -
27.🧰 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(12)); -
28.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(65, 5)); -
29.🧰 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(8) + fee(3)); -
30.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(1) + points(4)); -
31.🧰 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(11)); -
32.🧰 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(11)); -
33.🧰 What number does this code show?
function double(n) {
return n * 2;
}
show(double(25)); -
34.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(2) + points(2)); -
35.🧰 What number does this code show?
function points(stars) {
return stars * 10 + 5;
}
show(points(1) + points(1)); -
36.🧰 What number does this code show?
function total(price, qty) {
return price * qty;
}
show(total(95, 2)); -
37.🧰 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(7) + fee(7)); -
38.🧰 What number does this code show?
function triple(n) {
return n * 3;
}
show(triple(17)); -
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 points(stars) {
return stars * 10 + 5;
}
show(points(4) + points(2));
Answer key
- 10
- 20
- 20
- 100
- 80
- to reuse code
- 110
- 70
- 90
- 60
- 40
- 160
- 15
- Hello, Aman!
- sends back a value
- 20
- 33
- 120
- 14
- 210
- 425
- 200
- 200
- 0
- 20
- 80
- 100
- 325
- 50
- 60
- 40
- 40
- 50
- 50
- 30
- 190
- 120
- 51
- 50
- 70
Free app development lessons, practice and worksheets at talentjr.in/app-development-for-students