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