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