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