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