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