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