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