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