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