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