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