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