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