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