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