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