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