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