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