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