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