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