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