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