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