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