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