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