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