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