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