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