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