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