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