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