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