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