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