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