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