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