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