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