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